码迷,mamicode.com
首页 > 其他好文 > 详细

画画板小案例

时间:2019-01-10 22:59:58      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:void   protect   compress   factor   listener   mat   res   OLE   tps   

画画板小案例

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Paint paint;
    private Bitmap bitmapC;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button changC = findViewById(R.id.changeColor);
        changC.setOnClickListener(this);
        Button bold = findViewById(R.id.bold);
        bold.setOnClickListener(this);
        Button save = findViewById(R.id.save);
        save.setOnClickListener(this);
        final ImageView iv = findViewById(R.id.iv);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bg);
        bitmapC = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        paint = new Paint();
        final Canvas canvas = new Canvas(bitmapC);
        canvas.drawBitmap(bitmap, new Matrix(), paint);
        iv.setImageBitmap(bitmapC);
        iv.setOnTouchListener(new View.OnTouchListener() {
            float startX = 0;
            float startY = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float stopX = event.getX();
                        float stopY = event.getY();
                        canvas.drawLine(startX, startY, stopX, stopY, paint);
                        iv.setImageBitmap(bitmapC);
                        startX = stopX;
                        startY = stopY;
                        break;
                }
                return true;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.changeColor:
                paint.setColor(Color.RED);
                break;
            case R.id.bold:
                paint.setStrokeWidth(20f);
                break;
            case R.id.save:
                try {
                    File file = new File(getFilesDir().getParent(), "dazuo.png");
                    FileOutputStream fos = new FileOutputStream(file);
                    boolean compress = bitmapC.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    if (compress) {
                        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                    }
                } catch (FileNotFoundException e) {
                    Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                break;
        }
    }
}

代码已上传https://gitee.com/nangongyibin/Android_DrawingVersion

画画板小案例

标签:void   protect   compress   factor   listener   mat   res   OLE   tps   

原文地址:https://www.cnblogs.com/nangongyibin/p/10252645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!