倒影view和surfaceView的示例用法

来源:互联网 发布:下个会声会影软件 编辑:程序博客网 时间:2024/06/11 22:06

这里写图片描述
倒影View 原理:
Matrix matrix = new Matrix();
matrix.setScale(1F, -1F);

public class ReflectView extends View {    private Bitmap mSrcBitmap, mRefBitmap;    private Paint mPaint;    private PorterDuffXfermode mXfermode;    public ReflectView(Context context) {        super(context);        initRes(context);    }    public ReflectView(Context context, AttributeSet attrs) {        super(context, attrs);        initRes(context);    }    public ReflectView(Context context, AttributeSet attrs,                       int defStyleAttr) {        super(context, attrs, defStyleAttr);        initRes(context);    }    private void initRes(Context context) {        mSrcBitmap = BitmapFactory.decodeResource(getResources(),                R.drawable.test);        Matrix matrix = new Matrix();        matrix.setScale(1F, -1F);        mRefBitmap = Bitmap.createBitmap(mSrcBitmap, 0, 0,                mSrcBitmap.getWidth(), mSrcBitmap.getHeight(), matrix, true);        mPaint = new Paint();        mPaint.setShader(new LinearGradient(0, mSrcBitmap.getHeight(), 0,                mSrcBitmap.getHeight() + mSrcBitmap.getHeight() / 4,                0XDD000000, 0X10000000, Shader.TileMode.CLAMP));        mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);    }    @Override    protected void onDraw(Canvas canvas) {        canvas.drawColor(Color.BLACK);        canvas.drawBitmap(mSrcBitmap, 0, 0, null);        canvas.drawBitmap(mRefBitmap, 0, mSrcBitmap.getHeight(), null);        mPaint.setXfermode(mXfermode);        // 绘制渐变效果矩形        canvas.drawRect(0, mSrcBitmap.getHeight(),                mRefBitmap.getWidth(), mSrcBitmap.getHeight() * 2, mPaint);        mPaint.setXfermode(null);    }}

SurfaceView
在create方法中启动一个死循环draw 不停的绘制,拿画布的方式也比较特殊

public class SimpleDraw extends SurfaceView        implements SurfaceHolder.Callback, Runnable {    private SurfaceHolder mHolder;    private Canvas mCanvas;    private boolean mIsDrawing;    private Path mPath;    private Paint mPaint;    public SimpleDraw(Context context) {        super(context);        initView();    }    public SimpleDraw(Context context, AttributeSet attrs) {        super(context, attrs);        initView();    }    public SimpleDraw(Context context, AttributeSet attrs,                      int defStyle) {        super(context, attrs, defStyle);        initView();    }    private void initView() {        mHolder = getHolder();        mHolder.addCallback(this);        setFocusable(true);        setFocusableInTouchMode(true);        this.setKeepScreenOn(true);        mPath = new Path();        mPaint = new Paint();        mPaint.setColor(Color.RED);        mPaint.setStyle(Paint.Style.STROKE);        mPaint.setStrokeWidth(40);    }    @Override    public void surfaceCreated(SurfaceHolder holder) {        mIsDrawing = true;        new Thread(this).start();    }    @Override    public void surfaceChanged(SurfaceHolder holder,                               int format, int width, int height) {    }    @Override    public void surfaceDestroyed(SurfaceHolder holder) {        mIsDrawing = false;    }    @Override    public void run() {        long start = System.currentTimeMillis();        while (mIsDrawing) {            draw();        }        long end = System.currentTimeMillis();        // 50 - 100        if (end - start < 100) {            try {                Thread.sleep(100 - (end - start));            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    private void draw() {        try {            mCanvas = mHolder.lockCanvas();            mCanvas.drawColor(Color.WHITE);            mCanvas.drawPath(mPath, mPaint);        } catch (Exception e) {        } finally {            if (mCanvas != null)                mHolder.unlockCanvasAndPost(mCanvas);        }    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int x = (int) event.getX();        int y = (int) event.getY();        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                mPath.moveTo(x, y);                break;            case MotionEvent.ACTION_MOVE:                mPath.lineTo(x, y);                break;            case MotionEvent.ACTION_UP:                break;        }        return true;    }}
0 0
原创粉丝点击