Android 實現圖片畫畫板,

來源:互聯網
上載者:User

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();        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.