Scraping award in life often see, online now also has a variety of sweepstakes, below we are going to achieve a scraping music program, can perfect meet everyone's vanity, haha, the following start, 100% winning!
Let's take a look at the following:
Let's take a look at its layout:
1234567891011121314151617181920212223 |
<
RelativeLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
TextView
android:id
=
"@+id/textView1"
android:layout_width
=
"300dp"
android:layout_height
=
"150dp"
android:layout_centerHorizontal
=
"true"
android:layout_centerVertical
=
"true"
android:background
=
"@android:color/holo_red_dark"
android:gravity
=
"center"
android:text
=
"恭喜你,中奖啦!"
android:textSize
=
"35dp" />
<
net.androidchina.example.guale.EraseView
android:id
=
"@+id/eraseView1"
android:layout_width
=
"300dp"
android:layout_height
=
"150dp"
android:layout_centerHorizontal
=
"true"
android:layout_centerInParent
=
"true"
android:layout_centerVertical
=
"true"
/>
</
RelativeLayout
>
|
Layout is very simple, through the relative layout, the text and view overlap together, focusing on the implementation of the class Eraseview, the following look at the code of this class:
First we need to define a canvas and a brush
12345678910 |
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(
30
);
mCanvas =
new
Canvas(bitmap);
|
When your finger touches the screen, you need to trigger a slide event:
12345678910111213141516171819 |
@Override
public boolean
onTouchEvent(MotionEvent event) {
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);
}
|
A path is required here to record the trajectory, and invalidate () needs to be called, and the OnDraw method is further called to redraw the canvas:
12345678910 |
@Override
protected
void onDraw(Canvas canvas) {
if
(mCanvas ==
null
) {
EraseBitmp();
}
canvas.drawBitmap(bitmap,
0
,
0
,
null
);
mCanvas.drawPath(path, paint);
super
.onDraw(canvas);
}
|
SOURCE download: Click to download
An example of scraping music for Android development