Android 实现图片画画板_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android 实现图片画画板

Android 实现图片画画板

 2016/9/6 5:31:08  wuyudong  程序员俱乐部  我要评论(0)
  • 摘要:本文主要讲述了Android实现图片画画板设计项目布局:<RelativeLayoutxmlns: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
  • 标签:android 实现 图片

本文主要讲述了Android 实现图片画画板

设计项目布局:

<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" >

    <ImageView
        android:layout_above="@+id/bt"
        android:id="@+id/iv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/bt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="save"
        android:text="保存图片" />

</RelativeLayout>

首先实现画图功能:

public class MainActivity extends Activity {
    private ImageView iv;
    private Bitmap baseBitmap;
    private Canvas canvas;
    private Paint paint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
        paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.GREEN);
        
        // 创建一个可以被修改的bitmap
        baseBitmap = Bitmap.createBitmap(320, 360,
                Bitmap.Config.ARGB_8888);
        System.out.println("图宽度:"+iv.getWidth());
        System.out.println("图高度:"+iv.getHeight());
        canvas = new Canvas(baseBitmap);
        canvas.drawColor(Color.WHITE);

        // 知道用户手指在屏幕上移动的轨迹
        iv.setOnTouchListener(new OnTouchListener() {
            // 设置手指开始的坐标
            int startX;
            int startY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: // 手指第一次接触屏幕
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    break;
                case MotionEvent.ACTION_MOVE:// 手指在屏幕上滑动
                    int newX = (int) event.getX();
                    int newY = (int) event.getY();

                    canvas.drawLine(startX, startY, newX, newY, paint);
                    // 重新更新画笔的开始位置
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    iv.setImageBitmap(baseBitmap);
                    break;
                case MotionEvent.ACTION_UP: // 手指离开屏幕
                    break;

                default:
                    break;
                }
                return true;
            }
        });
    }

    public void save(View view) {

    }

}

接下来完成保存图片的功能:

    public void save(View view) {
        try {
            File file = new File(Environment.getExternalStorageDirectory(),
                    System.currentTimeMillis() + ".jpg");
            FileOutputStream stream = new FileOutputStream(file);
            baseBitmap.compress(CompressFormat.JPEG, 100, stream);
            stream.close();
            Toast.makeText(this, "保存图片成功", 1).show();
            
            //模拟一个消息通知系统sd卡被重新挂载了
            Intent intent = new Intent();
            intent.setAction(intent.ACTION_MEDIA_MOUNTED);
            intent.setData(Uri.fromFile(Environment
                    .getExternalStorageDirectory()));
            sendBroadcast(intent);

        } catch (Exception e) {
            Toast.makeText(this, "保存图片失败", 1).show();
            e.printStackTrace();
        }

    }
发表评论
用户名: 匿名