我想创建一个绘图应用程序。为此,我创建了一个Custom DrawingView。要在绘图期间启用其他选项,CustomView应包含在特定区域的activity_main.xml中。
CGRect circleRect = CGRectMake(circles[i][0] - circles[i][2] ,
circles[i][1] - circles[i][2],
2 * circles[i][2],
2 * circles[i][2]
);
public class MainActivity extends AppCompatActivity {
DrawingView dv ;
private Paint mPaint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.GREEN);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(12);
dv = new DrawingView(this, mPaint);
setContentView(R.layout.activity_main);
}
}
public class DrawingView extends View {
public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint, mPaint;
Context context;
private Paint circlePaint;
private Path circlePath;
public DrawingView(Context c, Paint pPaint) {
super(c);
context=c;
mPaint = pPaint;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
circlePaint = new Paint();
circlePath = new Path();
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
}
@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.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath( mPath, mPaint);
canvas.drawPath( circlePath, circlePaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
circlePath.reset();
circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
circlePath.reset();
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}

但是如果我尝试这个,总会抛出InflateException
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:gravity="center"
tools:context="de.sentoon.touchtesterv4.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
<de.sentoon.touchtesterv4.DrawingView
android:layout_width="match_parent"
android:layout_height="150sp" />
</LinearLayout>
感谢您的帮助。
答案 0 :(得分:0)
引起:java.lang.NoSuchMethodException:[class android.content.Context,interface android.util.AttributeSet]
您尚未声明足够的构造函数。
通常自定义视图至少有2个构造函数。一个接受Context
,另一个接受Context
和AttributeSet
。
public class DravingView extends View {
public DravingView(final Context context) {
super(context);
}
public DravingView(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
}
为什么我需要那个构造函数?
首先解析xml
,然后创建相应的类对象。解析布局xml
文件后,框架将尝试使用DrawingView
中的应用参数为您创建xml
对象。这些参数正在通过AttributeSet
传递。但只要它在DrawingView
类中找不到这样的构造函数,它就会崩溃。
答案 1 :(得分:0)
将您的DrawingView
构造函数更改为此
public DrawingView(Context c, AttributeSet attr) {
super(c, attr);
context=c;
}
将此方法添加到DrawingView
类
public void setPaint(Paint pPaint) {
mPaint = pPaint;
}
在xml中向图纸视图添加ID
<de.sentoon.touchtesterv4.DrawingView
android:id="@+id/drawingView"
android:layout_width="match_parent"
android:layout_height="150sp" />
然后像这样初始化drawingView
dv = (DrawingView) findViewById(R.id.drawingView);
然后在Activity中设置你的画颜
dv.setPaint(mPaint);