GuaGuaKA类文件
class GuaGuaKa extends View {
private Canvas mCanvas = null;
private Path mPath = null;
private Paint mPaint = null;
private Bitmap bitmap_temp = null;
private Bitmap bitmap = null;
private Boolean mCompleted = false;
int x = 0;
int y = 0;
private OnCaCompleteListener mOnCompleteListener;
public GuaGuaKa(Context context,
OnCaCompleteListener mOnCompleteListener) {
super(context);
init(context);
this.mOnCompleteListener = mOnCompleteListener;
}
private void init(Context context) {
mPath = new Path();
mPaint = new Paint();
mPaint.setAlpha(0);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(120);
bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.bg_jiyudianjin_2), screenWidth,
screenHeight, true);
bitmap_temp = Bitmap.createBitmap(screenWidth, screenHeight,
Bitmap.Config.ARGB_8888);
mCanvas = new Canvas();
mCanvas.setBitmap(bitmap_temp);
mCanvas.drawBitmap(bitmap, 0, 0, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap_temp, 0, 0, null);
mCanvas.drawPath(mPath, mPaint);
if (mCompleted) {
mOnCompleteListener.OnCompleted();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
int currX = (int) event.getX();
int currY = (int) event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN: {
mPath.reset();
x = currX;
y = currY;
mPath.moveTo(x, y);
}
break;
case MotionEvent.ACTION_MOVE: {
mPath.quadTo(x, y, currX, currY);
x = currX;
y = currY;
postInvalidate();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
mPath.reset();
new Thread(mRunnable).start();
}
break;
}
return true;
}
/**
* 起一个线程来计算已经扫的面积及占总区域的比例 根据区域来判断是否完成
*/
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
int w = getWidth();
int h = getHeight();
float wipeArea = 0;
float totalArea = w * h;
Bitmap mbitmap = bitmap_temp;
int[] mPixels = new int[w * h];
mbitmap.getPixels(mPixels, 0, w, 0, 0, w, h);
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++) {
int index = i + j * w;
if (mPixels[index] == 0) {
wipeArea++;
}
}
if (wipeArea > 0 && totalArea > 0) {
int percent = (int) (wipeArea * 100 / totalArea);
if (percent > 10) {
mCompleted = true;
postInvalidate();
}
}
};
};
}
public interface OnCaCompleteListener {
public void OnCompleted();
}
布局文件
<RelativeLayout
android:id="@+id/layout_anim"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_m"
android:orientation="vertical"
android:visibility="gone" >
</RelativeLayout>
使用
mAnimLayout.removeAllViews()
//创建一个
final TextView txt = new TextView(UtilJiYuActivity.this)
txt.setText(resultTitle)
txt.setTextSize(30)
txt.setTextColor(getResources().getColor(
R.color.color_util_jydj_txt_color))
txt.setGravity(Gravity.CENTER)
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
lp.setMargins(0, 25, 0, 25)
txt.setLayoutParams(lp)
mAnimLayout.addView(txt)
mAnimLayout.addView(new GuaGuaKa(UtilJiYuActivity.this,
new OnCaCompleteListener() {
@Override
public void OnCompleted() {
mAnimLayout.setVisibility(View.GONE)
mResultLayout.setVisibility(View.VISIBLE)
}
}))