【发布时间】:2014-04-25 04:03:08
【问题描述】:
到目前为止,我通过扩展 ImageView 类完成了自定义视图。
现在,
我必须为屏幕上的绘图和 2 个按钮创建自定义视图。
我必须分开显示按钮和自定义视图。按钮不应超过自定义视图。
怎么做..?
需要你的帮助............
我的示例代码..
package com.androiddom.customview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CustomView extends ImageView {
private Paint paint = new Paint();
private Path path = new Path();
public CustomView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(15f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// Gives you x and y coordinates on the Event.
float pointX = event.getX();
float pointY = event.getY();
// Checks for the event that occurs
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
case MotionEvent.ACTION_UP:
XYZ etc...
break;
default:
return false;
}
postInvalidate();
return true;
}
public void clear() {
//Path path = new Path();
path.reset();
invalidate();
}
}
目前我正在从主要活动中调用 customview 类,如以下方法:
MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomeView tv = (CustomeView)
setContentView(tv);
}
}
【问题讨论】:
-
我必须创建绘图应用程序,触摸手指线将被绘制..
标签: android view android-canvas android-view android-custom-view