android 简单的画图片

layout:

<LinearLayout 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:orientation="vertical"
    tools:context="com.example.paint.MainActivity" >

    <Button
        android:onClick="save"
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存" />
    <Button
        android:onClick="selectImage"
        android:id="@+id/select"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="选择一个图片" />

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

main_activity

package com.example.paint;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    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);
        
        paint = new Paint();
        //画笔宽度
        paint.setStrokeWidth(5);
        //画笔颜色
        paint.setColor(Color.GREEN);
        
        //创建一个可以修改的bitmap
        baseBitmap = Bitmap.createBitmap(320, 350, Config.ARGB_8888);
        //创建一个画布
        canvas = new Canvas(baseBitmap);
        //设置背景色
        canvas.drawColor(Color.WHITE);
        
        iv = (ImageView)findViewById(R.id.iv);
        
        //知道用户手指在平美国上移动的轨迹
        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, newY, newY, paint);
                        
                        //重新更新画笔开始位置
                        startX =  (int)event.getX();
                        startY = (int)event.getY();                        
                        iv.setImageBitmap(baseBitmap);
                        break;
                    case MotionEvent.ACTION_UP: //手指离开屏幕的动作
                        
                        break;
                
                }
                
                //return true 表示画笔一直触动
                return true;
            }

            
        });
    }
    
    
    
    public void selectImage(View view)
    {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, 0);
        
    }
    
    @Override
    protected void onActivityResult(int arg0, int arg1, Intent data) {
        if(data!=null)
        {
            
            Uri uri = data.getData();
            Bitmap bitmap = data.getParcelableExtra("data");
            baseBitmap = bitmap;
            iv.setImageURI(uri);
            
        }
        super.onActivityResult(arg0, arg1, data);
    }
    
    
    public void save(View view)
    {   
        try {
            
            File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
            FileOutputStream fos = new FileOutputStream(file);
            //保存图片
            baseBitmap.compress(CompressFormat.JPEG, 100, fos);
            
            Toast.makeText(this, "保存图片成功", 0).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, "保存图片失败", 0).show();
            e.printStackTrace();
        }
        
    }


  
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。