android 实现在照片上涂鸦

图片上涂鸦

在android设备上对照片进行涂鸦,要解决的不同手机屏幕大小不一致的问题,怎样做才能去适应不同的手机屏幕,这里,可以用到一个相似的数学知识。
就是,获取手机屏幕的像素值,再得到图片大小的像素值,计算出两个值的比值,然后监听触摸事件,监听触摸事件得到的数据,处理一下,就可对应到图片具体的像素点。看一下效果。



好了,看一下代码
public class ScrawlActivity extends Activity {
    ImageView imageView;
    double pictureRelativeLeft, pictureRelativeTop, pictureRelativeRight, 
            pictureRelativeButtom;
    double imageViewLeft, imageViewTop, imageViewRight, imageViewButtom; 
    double pictureRealLeft, pictureRealTop, pictureRealRight, 
            pictureRealButtom;
    Bitmap bitmap;
    double proportionWidth, proportionHeight; 
    double bitmapWidth, bitmapHeight; 
    Canvas canvas;
    Path path;
    double preX, preY;
    Paint paint;
    boolean hasOut=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InitToolBar.initToolBar(this);
        setContentView(R.layout.activity_scrawl);
        setActionBar();
        imageView = (ImageView) findViewById(R.id.scrawlImageView);

        BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
        bfoOptions.inScaled = false;
        Intent intent=getIntent();

        bitmap=((APP)getApplication()).bitmap.copy(Bitmap.Config.ARGB_8888, true);
        imageView.setImageBitmap(bitmap);
        bitmapWidth = bitmap.getWidth();
        bitmapHeight = bitmap.getHeight(); 
        canvas = new Canvas();
        System.out.println(bitmap);
        canvas.setBitmap(bitmap);
        setPiont();
        path = new Path();
        System.out.println("bitmap:   " + bitmapWidth + "     " + bitmapHeight);
    }
    void setActionBar(){
        ActionBar actionBar=getActionBar();
        actionBar.setTitle(" ");
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.actionbar));
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus == true) {

            Matrix matrix = imageView.getImageMatrix();
            Rect rect = imageView.getDrawable().getBounds();
            float[] values = new float[9];
            matrix.getValues(values);
            pictureRelativeLeft = values[2];
            pictureRelativeTop = values[5];
            pictureRelativeRight = pictureRelativeLeft + rect.width()
                    * values[0];
            pictureRelativeButtom = pictureRelativeTop + rect.height()
                    * values[0];

            int[] location = new int[2]; // 获取组件在手机屏幕中的绝对像素位置
            imageView.getLocationOnScreen(location);
            imageViewLeft = location[0];
            imageViewTop = location[1];
            System.out.println("imageView:" + imageViewLeft + "     "
                    + imageViewTop);


            imageViewRight = imageView.getRight();
            imageViewButtom = imageView.getBottom();
            setPictureRealPosition();

            proportionWidth = bitmapWidth
                    / (pictureRealRight - pictureRealLeft);
            proportionHeight = bitmapHeight
                    / (pictureRealButtom - pictureRealTop);

        }
    }

    void setPiont() {
        // 设置画笔的颜色
        paint = new Paint(Paint.DITHER_FLAG);
        paint.setColor(Color.RED);
        // 设置画笔风格
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        // 反锯齿
        paint.setAntiAlias(true);
        paint.setDither(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        double x = event.getX();
        double y = event.getY();
        if (x >= pictureRealLeft && x <= pictureRealRight
                && y >= pictureRealTop && y <= pictureRealButtom) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = (x - pictureRealLeft) * proportionWidth;
                y = (y - pictureRealTop) * proportionHeight;
                path.moveTo((float) x, (float) y);
                preX = x;
                preY = y;
                break;

            case MotionEvent.ACTION_MOVE:

                System.out.println(x + "     " + y);
                x = (x - pictureRealLeft) * proportionWidth;
                y = (y - pictureRealTop) * proportionHeight;
                if(hasOut==true){
                    path.reset();
                    path.moveTo((float) x, (float) y);
                    preX=x;
                    preY=y;
                    System.out.println("reset");
                    hasOut=false;
                }
                path.quadTo((float) preX, (float) preY, (float) x, (float) y);
                preX = x;
                preY = y;
                break;

            case MotionEvent.ACTION_UP:
                System.out.println(x + "     " + y);
                x = (x - pictureRealLeft) * proportionWidth;
                y = (y - pictureRealTop) * proportionHeight;
                canvas.drawPath(path, paint); // ①
                path.reset();
                break;

            }
        } else {
            path.reset();
            hasOut=true;
            //System.out.println("reset");
        }
        // 将cacheBitmap绘制到该View组件上
        //canvas.drawBitmap(bitmap, 0, 0, paint); // ②
        // 沿着path绘制
        canvas.drawPath(path, paint);
        imageView.setImageBitmap(bitmap);
        // invalidate();

        return false;
    }

    void setPictureRealPosition() {
        pictureRealLeft = imageViewLeft + pictureRelativeLeft;
        pictureRealTop = imageViewTop + pictureRelativeTop;
        pictureRealRight = imageViewLeft + pictureRelativeRight;
        pictureRealButtom = imageViewTop + pictureRelativeButtom;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.scrawl, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.scrawlSure) {
            Intent intent =new Intent();
            intent.putExtra("result", true);
            setResult(1, intent);
            ((APP)getApplication()).bitmap=bitmap;
            finish();
            return true;
        }
        if(id==R.id.scrawlCancel){
            setResult(2);
            finish();
            return true;
        }
        if(id==android.R.id.home){
            setResult(2);
            finish();
            return true;
        }
        return false;
    }
    @Override
    protected void onDestroy() {

        super.onDestroy();
        setResult(2);
    }
}    
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值