안 드 로 이 드 스 크 래 치 효과 컨트롤 공유
스크레이퍼 종류:
package com.reyo.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
/**
*
* @author reyo
*
*/
public class ScratchTextView extends TextView{
private float TOUCH_TOLERANCE;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
private Path mPath;
private float mX,mY;
private boolean isDraw = false;
public ScratchTextView(Context context) {
super(context);
}
public ScratchTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ScratchTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isDraw) {
mCanvas.drawPath(mPath, mPaint);
// mCanvas.drawPoint(mX, mY, mPaint);
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
/**
*
* @param bgColor
* @param paintStrokeWidth
* @param touchTolerance
*/
public void initScratchCard(final int bgColor,final int paintStrokeWidth,float touchTolerance) {
TOUCH_TOLERANCE = touchTolerance;
mPaint = new Paint();
// mPaint.setAlpha(0);
// mPaint.setColor(Color.BLACK);
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
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(paintStrokeWidth);
mPath = new Path();
mBitmap = Bitmap.createBitmap(getLayoutParams().width, getLayoutParams().height, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mCanvas.drawColor(bgColor);
isDraw = true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isDraw) {
return true;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchDown(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp(event.getX(), event.getY());
invalidate();
break;
default:
break;
}
return true;
}
private void touchDown(float x,float y){
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(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;
}
}
private void touchUp(float x,float y){
mPath.lineTo(x, y);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
}
사용:
package com.reyo.app;
import com.reyo.view.ScratchTextView;
import android.app.Activity;
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;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
public class RubberActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(new RubberView(this,"title",new Rect(100, 200,300,250),5,1f,18));
setContentView(R.layout.main);
((ScratchTextView) findViewById(R.id.rubbler)).initScratchCard(0xFFFFFFFF, 5, 1f);
}
class RubberView extends View {
private final int PAINT_STROKE_WIDTH;
private final float TOUCH_TOLERANCE;
private final int TEXT_SIZE;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mPaint;
private Path mPath;
private float mX, mY;
private final int X, Y, W, H;
private final Rect touchRect;
public RubberView(Context context, String bgText, Rect rect,
int paintStrokeWidth, float touchTolerance, int textSize) {
super(context);
setFocusable(true);
touchRect = rect;
W = rect.right - rect.left;
H = rect.bottom - rect.top;
X = rect.left;
Y = rect.top;
TEXT_SIZE = textSize;
PAINT_STROKE_WIDTH = paintStrokeWidth;
TOUCH_TOLERANCE = touchTolerance;
setBackground(touchRect, bgText);
initDrowTools();
}
private void setBackground(Rect rect, String bgText) {
DisplayMetrics dm = new DisplayMetrics();
dm = this.getResources().getDisplayMetrics();
Bitmap bitmap = Bitmap.createBitmap(dm.widthPixels,
dm.heightPixels, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(0x88000000);
// paint.setStyle(Style.STROKE);
// paint.setTextAlign(Align.CENTER);
paint.setTextSize(TEXT_SIZE);
// paint.setTextScaleX(1.5f);
canvas.drawColor(Color.WHITE);
int x = rect.left + (rect.right - rect.left - bgText.length() * TEXT_SIZE) / 2;
int y = rect.top + (rect.bottom - rect.top - TEXT_SIZE) / 2;
// int y = 218+25;
canvas.drawText(bgText, x, y, paint);
Drawable drawable = new BitmapDrawable(bitmap);
setBackgroundDrawable(drawable);
}
private void initDrowTools() {
mPaint = new Paint();
// mPaint.setAlpha(0);
// mPaint.setColor(Color.BLACK);
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
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(PAINT_STROKE_WIDTH);
mPath = new Path();
mBitmap = Bitmap.createBitmap(W, H, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mCanvas.drawColor(0x88000000);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mCanvas.drawPath(mPath, mPaint);
// mCanvas.drawPoint(mX, mY, mPaint);
canvas.drawBitmap(mBitmap, X, Y, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!touchRect.contains((int) event.getX(), (int) event.getY())) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchDown(event.getRawX(), event.getRawY());
touchDown(event.getX() - touchRect.left, event.getY()
- touchRect.top);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(event.getX() - touchRect.left, event.getY()
- touchRect.top);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp(event.getX() - touchRect.left, event.getY()
- touchRect.top);
invalidate();
break;
default:
break;
}
return true;
}
private void touchDown(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(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;
}
}
private void touchUp(float x, float y) {
mPath.lineTo(x, y);
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
}
}
레이아웃 xml 코드:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--
android:layout_width
android:layout_height
-->
<com.reyo.view.ScratchTextView
android:id="@+id/rubbler"
android:layout_y="100dip"
android:layout_x="100dip"
android:layout_width="100dip"
android:layout_height="50dip"
android:text=" "
android:textSize="18sp"
android:gravity="center"
/>
</AbsoluteLayout>
스 크 래 치 효과 의 컨트롤:https://github.com/winsontan520/Android-WScratchView이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.