Android刮刮乐

本文介绍了一种自定义的Android View组件——EraseView,该组件允许用户通过触摸屏幕来擦除预先设定的背景图案。文章详细展示了EraseView的实现原理,包括如何响应用户的触摸事件,并使用画布和路径绘制技术来模拟擦除效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在UI页面的布局文件中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:text="安慰奖"
        android:textSize="40sp" />

    <com.example.guale.EraseView
        android:id="@+id/eraseView1"
        android:layout_width="match_parent"
        android:layout_height="150dp" />

</RelativeLayout>

对应的com.example.guale.EraseView文件:

package com.example.guale;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class EraseView extends View {
	
	//用户已经挂过的标记
	private boolean isMove = false;
	private Bitmap bitmap = null;
	private Bitmap frontBitmap = null;
	private Path path;
	private Canvas mCanvas;
	private Paint paint;
	
	
	public EraseView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		
	    if (mCanvas == null) {
		    EraseBitmp();
	    } 
		canvas.drawBitmap(bitmap, 0, 0, null);	
		mCanvas.drawPath(path,paint);
		super.onDraw(canvas);
	}
	
	public void EraseBitmp() {
		
		bitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_4444);
		
		frontBitmap = CreateBitmap(Color.GRAY,getWidth(),getHeight());

		paint = new Paint();
		paint.setStyle(Paint.Style.STROKE);
		paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
		paint.setAntiAlias(true);
		paint.setDither(true);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(40);
		
		path = new Path();

		mCanvas = new Canvas(bitmap);
		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);
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值