App绘制android

时间:2017-02-15 10:53:03

标签: java android android-canvas

我有两个类,一个包含按钮,另一个画布可以绘制,

我想在点击按钮圈时开始绘画

图纸必须从屏幕中间开始

MainActivity

public class MainActivity extends AppCompatActivity {

CanvasView canvasview ;
Button circle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    canvasview = (CanvasView) findViewById(R.id.canvas);
    circle = (Button) findViewById(R.id.circle);      
    DisplayMetrics metrics = this.getResources().getDisplayMetrics();
    int x = metrics.widthPixels;
    int y = metrics.heightPixels;

    circle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Abbes", Toast.LENGTH_SHORT).show();

            View j = findViewById(R.id.circle);
            j.setVisibility(View.GONE);
        }
    });
}

CanvasView.java

public class CanvasView extends View {
public int widht;
public int height;
private Bitmap mbitmap;
private Canvas mcanvas;
private Path path;
private Paint paint;
private float mX, mY;
Context context;
private static final float Tolerance = 5;

public CanvasView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    path = new Path();
    paint = new Paint();
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(20f);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mbitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
    mcanvas = new Canvas(mbitmap);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path,paint);
}

private void startTouch (float x , float y){
path.moveTo(x,y);
    mX = x;
    mY = y ;
}
public void moveTouche (float x,float y ) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if(dx >= Tolerance || dy >= Tolerance){
        path.quadTo(mX,mY,(x+mX)/2,(y+mY)/2);
        mX = x ;
        mY = y;

    }
}
private void upTouch(){
    path.lineTo(mX,mY);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            startTouch(x,y);
            invalidate();
            break ;
        case MotionEvent.ACTION_UP:
            upTouch();
            invalidate();
            break ;
        case MotionEvent.ACTION_MOVE:
            moveTouche(x,y);
            invalidate();
            break ;

    }
    return true ;
}
}

XML

  <Button
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:background="@drawable/round_button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/circle"  />
<com.example.amgsoft_pc.flowermenu.CanvasView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/canvas"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"/>

0 个答案:

没有答案