我想要制作划痕效果的代码如下:
public class Test extends View {
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
private boolean isMove = false;
private Bitmap bitmap = null;
private Bitmap frontBitmap = null;
private Path path;
private Canvas mCanvas;
private Paint paint;
protected void onDraw(Canvas canvas) {
if (mCanvas == null) {
EraseBitmp();
}
canvas.drawBitmap(bitmap, 0, 0, null);
mCanvas.drawPath(path,paint);//Draw a path
super.onDraw(canvas);
}
public void EraseBitmp() {
bitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_4444);
frontBitmap = CreateBitmap(Color.GRAY,getWidth(),getHeight());//The production of grey.
//Set the brush style
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);//Set the brush style: Hollow
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//Display mode settings when photo cross
paint.setAntiAlias(true);//Anti according to tooth
paint.setDither(true);//Anti jitter
paint.setStrokeJoin(Paint.Join.ROUND);//Set the joint appearance, ROUND arc
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(20);//Set the stroke width
path = new Path();
mCanvas = new Canvas(bitmap);//Set up a band picture of canvas
mCanvas.drawBitmap(frontBitmap, 0, 0,null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float ax = event.getX();
float ay = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
isMove = false;
path.reset();
path.moveTo(ax, ay);
invalidate();
return true;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
isMove = true;
path.lineTo(ax,ay);
invalidate();
return true;
}
return super.onTouchEvent(event);
}
public Bitmap CreateBitmap(int color,int width, int height) {
int[] rgb = new int [width * height];
for (int i=0;i<rgb.length;i++) {
rgb[i] = color;
}
return Bitmap.createBitmap(rgb, width, height,Config.ARGB_8888);
}
}
和我声明该类的清单文件是:
<activity
android:name=".Test"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
当我想编译时会出现上述错误:无法实例化类;没有空的构造函数 我添加了一个空构造函数但没有成功。我是新手所以请耐心等待。我在网上搜索但无法得到解决方案。
和xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="100dp"
>
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="150dp"
android:textSize="40dp"
android:gravity="center_vertical|center_horizontal"
android:text="Thanks for your patronage"
android:background="#000"
/>
<com.example.winter.carrefour.Test
android:id="@+id/eraseView1"
android:layout_width="300dp"
android:layout_height="150dp"/>
</RelativeLayout>
</LinearLayout>
答案 0 :(得分:3)
当我想编译时会出现上述错误:无法实例化类;没有空构造函数我添加了一个空构造函数但没有成功
您发布的代码中没有必需的构造函数。你只有一个,而你需要三个:
public Test(Context context) {
super(context);
}
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Test(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
修改强>
测试;没有空构造函数
空构造函数是这样的:
public Test() {}
然而它闻起来你尝试使用这种错误的方式。您是不是想尝试将其用作片段?
答案 1 :(得分:0)
你只有1个构造函数创建额外的构造函数并且没问题
public Test(Context context) {
super(context);
}
public Test(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public Test(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
答案 2 :(得分:0)
您的Test类只有一个构造函数
这个:
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
您需要默认值(构造函数不带参数)