我想在白色画布上绘制红色圆圈,但它是黑色的,只有在第一次触摸后变成白色,所以我希望它在开始时是白色的。这是我的代码。 (我也想知道浮动按钮出现的原因:D)
DrawingView.java:
class DrawingView extends SurfaceView {
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final SurfaceHolder surfaceHolder;
private ArrayList<Point> points;
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.RED);
surfaceHolder = getHolder();
paint.setStyle(Paint.Style.FILL);
setFocusable(true);
setFocusableInTouchMode(true);
points = new ArrayList<>();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
points.add(new Point((int) event.getX(), (int) event.getY()));
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
for (Point p : points)
canvas.drawCircle(p.x, p.y, 50, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
return false;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Save")
.setIcon(R.drawable.ic_save)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// do some magic to save :D
return false;
}
})
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
fab:menu_fab_size="normal"
fab:menu_showShadow="true"
fab:menu_shadowColor="#66000000"
fab:menu_shadowRadius="4dp"
fab:menu_shadowXOffset="1dp"
fab:menu_shadowYOffset="3dp"
fab:menu_colorNormal="#DA4336"
fab:menu_colorPressed="#E75043"
fab:menu_colorRipple="#99FFFFFF"
fab:menu_animationDelayPerItem="50"
fab:menu_icon="@drawable/fab_add"
fab:menu_buttonSpacing="0dp"
fab:menu_labels_margin="0dp"
fab:menu_labels_showAnimation="@anim/fab_slide_in_from_right"
fab:menu_labels_hideAnimation="@anim/fab_slide_out_to_right"
fab:menu_labels_paddingTop="4dp"
fab:menu_labels_paddingRight="8dp"
fab:menu_labels_paddingBottom="4dp"
fab:menu_labels_paddingLeft="8dp"
fab:menu_labels_padding="8dp"
fab:menu_labels_textColor="#FFFFFF"
fab:menu_labels_textSize="14sp"
fab:menu_labels_cornerRadius="3dp"
fab:menu_labels_colorNormal="#333333"
fab:menu_labels_colorPressed="#444444"
fab:menu_labels_colorRipple="#66FFFFFF"
fab:menu_labels_showShadow="true"
fab:menu_labels_singleLine="false"
fab:menu_labels_ellipsize="none"
fab:menu_labels_maxLines="-1"
fab:menu_labels_position="left"
fab:menu_openDirection="up"
fab:menu_backgroundColor="@android:color/transparent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/insertPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_insert_photo_24dp"
fab:fab_size="mini"
fab:fab_label="Add photos" />
<com.github.clans.fab.FloatingActionButton
android:id="@+id/addCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_brightness_1_24dp"
fab:fab_size="mini"
fab:fab_label="Add circles" />
</com.github.clans.fab.FloatingActionMenu>
<reminder.com.paint.DrawingView
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 0 :(得分:0)
我认为解决方法是在创建时使用,并在DrawingView.java中将颜色初始化为白色
public onCreate(...)
{
...
Set Color to white
...
}
继承人编辑: 真正的解决方案: 在构造函数中使用setBackground函数并将其设置为白色
ClassName(...)
{
Setbackground("WHITE")
}