<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <!--    <com.example.rubbler.Text_Rubbler        android:id="@+id/rubbler"        android:layout_width="200dp"        android:layout_height="200dp"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:gravity="center"        android:text="@string/hello_world" />    -->    <com.example.rubbler.Image_Rubbler        android:id="@+id/rubbler"        android:background="@drawable/girl1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:gravity="center" /></RelativeLayout>
package com.example.rubbler;import com.example.rubbler.R;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //Text_Rubbler rubbler =  (Text_Rubbler)this.findViewById(R.id.rubbler);        Image_Rubbler rubbler = (Image_Rubbler) this.findViewById(R.id.rubbler);        rubbler.beginRubbler(0xffff0000, 40, 50);    }    @Override    public boolean onCreateOptionsMenu(Menu menu)    {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}
package com.example.rubbler;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.util.AttributeSet;import android.view.MotionEvent;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.LinearInterpolator;import android.widget.ImageView;public class Image_Rubbler extends ImageView{    private float TOUCH_TOLERANCE; //填充距离,使线条更自然,柔和,值越小,越柔和。    //  private final int bgColor;    private Bitmap mBitmap;    private Canvas mCanvas;    private Paint mPaint;    private Path mPath;    private float mX, mY;    private boolean isDraw = false;    public Image_Rubbler(Context context)    {        super(context);    }    public Image_Rubbler(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);        //      bgColor = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "textColor", 0xFFFFFF);        //      System.out.println("Color:"+bgColor);    }    public Image_Rubbler(Context context, AttributeSet attrs)    {        super(context, attrs);        //      bgColor = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "textColor", 0xFFFFFF);        //      System.out.println(bgColor);        //      System.out.println(attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_width"));    }    @Override    protected void onDraw(Canvas canvas)    {        super.onDraw(canvas);        if (isDraw)        {            mCanvas.drawPath(mPath, mPaint);            canvas.drawBitmap(mBitmap, 0, 0, null);        }    }    /**     * 开启檫除功能     * @param bgColor 覆盖的背景颜色     * @param paintStrokeWidth 触点(橡皮)宽度     * @param touchTolerance 填充距离,值越小,越柔和。     */    public void beginRubbler(final int bgColor , final int paintStrokeWidth , float touchTolerance)    {        TOUCH_TOLERANCE = touchTolerance;        //设置画笔        mPaint = new Paint();        //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();        //覆盖        //      if (getLayoutParams().width == LayoutParams.FILL_PARENT) {        //                  //      }        int width = this.getResources().getDrawable(R.drawable.girl1).getIntrinsicWidth();        int height = this.getResources().getDrawable(R.drawable.girl1).getIntrinsicHeight();        mBitmap = Bitmap.createBitmap(width, 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.getRawX(),event.getRawY());                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();        int w = mBitmap.getWidth();        int h = mBitmap.getHeight();        int[] pixels = new int[w * h];        try        {            /*                                     参数            pixels      接收位图颜色值的数组            offset      写入到pixels[]中的第一个像素索引值            stride      pixels[]中的行间距个数值(必须大于等于位图宽度)。可以为负数            x           从位图中读取的第一个像素的x坐标值。            y           从位图中读取的第一个像素的y坐标值            width       从每一行中读取的像素宽度            height    读取的行数            */            mBitmap.getPixels(pixels, 0, w, 0, 0, w, h);            int rubblerCount = 0;            int bitmapCount = pixels.length;            for (int i = 0; i < bitmapCount; i++)            {                if (pixels[i] != 0xffff0000)                {                    rubblerCount++;                }            }            if (rubblerCount > bitmapCount * 0.3)            {                setFlickerAnimation(this);                Paint paint = new Paint();                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));                mCanvas.drawPaint(paint);                isDraw = false;            }        }        catch (Exception e)        {            e.printStackTrace();        }    }    //闪烁    private void setFlickerAnimation(ImageView iv_chat_head)    {        final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible          animation.setDuration(50); // duration - half a second          animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate          //        animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely         //        animation.setRepeatCount(2); // Repeat animation infinitely          animation.setRepeatMode(Animation.REVERSE); //           iv_chat_head.setAnimation(animation);    }}

package com.example.rubbler;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.util.AttributeSet;import android.view.MotionEvent;import android.widget.TextView;public class Text_Rubbler extends TextView{    private float TOUCH_TOLERANCE; //填充距离,使线条更自然,柔和,值越小,越柔和。    //  private final int bgColor;    private Bitmap mBitmap;    private Canvas mCanvas;    private Paint mPaint;    private Path mPath;    private float mX, mY;    private boolean isDraw = false;    public Text_Rubbler(Context context)    {        super(context);    }    public Text_Rubbler(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);        //      bgColor = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "textColor", 0xFFFFFF);        //      System.out.println("Color:"+bgColor);    }    public Text_Rubbler(Context context, AttributeSet attrs)    {        super(context, attrs);        //      bgColor = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "textColor", 0xFFFFFF);        //      System.out.println(bgColor);        //      System.out.println(attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_width"));    }    @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 beginRubbler(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();        ;        //覆盖        //      if (getLayoutParams().width == LayoutParams.FILL_PARENT) {        //                  //      }        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.getRawX(),event.getRawY());                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();    }}



更多相关文章

  1. Android(安卓)Shape详解
  2. android AlertDialog自定义全屏显示
  3. 学习Android界面开发: Shape Drawable
  4. 头条Android(安卓)屏幕适配
  5. Android(安卓)Button 样式 圆角
  6. 登录时旋转等待效果
  7. 关于android常用的按下高亮的展示
  8. android textview xml 属性设置
  9. Android:控件GridView的使用

随机推荐

  1. Android(安卓)用ViewFlipper实现各种切换
  2. XML Attributes(XML属性)
  3. android:gravity 和android:layout_gravit
  4. 浅析Android(安卓)手机卫士设备管理权限
  5. Android内核学习笔记
  6. Android,可以不再拷代码了吗?
  7. Android资源汇总贴
  8. Android技能树 — 数组,链表,散列表基础
  9. Android各控件常用属性
  10. android:gravity 和android:layout_gravit