Android實現(xiàn)畫畫板案例
更新時間:2019年01月28日 14:06:15 作者:常利兵
這篇文章主要為大家詳細介紹了Android實現(xiàn)畫畫板案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android實現(xiàn)畫畫板的具體代碼,供大家參考,具體內(nèi)容如下
① 準備一個布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.it.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="修改顏色"
android:onClick="color"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="粗細+1"
android:onClick="size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="保存圖片"
android:onClick="save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="@+id/iv_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
② 核心代碼
package com.example.it;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private final String TAG = getClass().getSimpleName();
private ImageView iv_show;
private Bitmap copyBm;
private float x;
private float y;
private Paint paint;
private int paintsize = 1;
private FileOutputStream fos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
iv_show = (ImageView) findViewById(R.id.iv_bg);
//創(chuàng)建一個空白的bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
copyBm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//創(chuàng)建畫布
final Canvas canvas = new Canvas(copyBm);
//使用畫布繪制圖片
paint = new Paint();
canvas.drawBitmap(bitmap,new Matrix(), paint);
iv_show.setImageBitmap(copyBm);
//為畫筆在屏幕上移動設置監(jiān)聽事件
iv_show.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//獲取當前事件的類型
int action = event.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
//如果是手指按下時,記錄一下手指的位置
x = event.getX();
y = event.getY();
Log.e(TAG,x+"******"+y);
break;
case MotionEvent.ACTION_MOVE:
//如果手指正在移動時
float x1 = event.getX();
float y1 = event.getY();
canvas.drawLine(x,y,x1,y1, paint);
x = x1;
y = y1;
Log.e(TAG,x1+"**********"+y1);
break;
case MotionEvent.ACTION_UP:
break;
}
iv_show.setImageBitmap(copyBm);
return true;
}
});
}
//修改顏色
public void color(View view) {
paint.setColor(Color.RED);
}
//修改線條粗細
public void size(View view) {
paintsize+=1;
paint.setStrokeWidth(paintsize);
}
//保存圖片
public void save(View view) {
//設置圖片保存的位置
File file = new File(Environment.getExternalStorageDirectory(), SystemClock.currentThreadTimeMillis()+".png");
try {
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//設置參數(shù)保存的質(zhì)量
copyBm.compress(Bitmap.CompressFormat.PNG,100,fos);
//定義一個意圖,通過發(fā)出廣播告訴系統(tǒng)掃描指定的位置的文件
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
}
③ 權限信息
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android有效獲取狀態(tài)欄(StatusBar)高度的方法
這篇文章主要介紹了Android有效獲取狀態(tài)欄(StatusBar)高度的方法,涉及Android針對狀態(tài)欄(StatusBar)屬性操作的相關技巧,需要的朋友可以參考下2016-08-08
Android編程實現(xiàn)滑動開關組件功能【附源碼下載】
這篇文章主要介紹了Android編程實現(xiàn)滑動開關組件功能,結合實例形式詳細分析了Android滑動開關組件的簡單布局與功能實現(xiàn)技巧,并附帶完整實例源碼供讀者下載參考,需要的朋友可以參考下2018-01-01
Android React Native原生模塊與JS模塊通信的方法總結
這篇文章主要介紹了Android React Native原生模塊與JS模塊通信的方法總結的相關資料,需要的朋友可以參考下2017-02-02
Android布局之絕對布局AbsoluteLayout詳解
這篇文章主要為大家詳細介紹了Android布局之絕對布局AbsoluteLayout的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)布局中為控件添加選擇器的方法,涉及Android開發(fā)中布局設置的相關操作技巧,需要的朋友可以參考下2017-10-10

