Android畫畫板的制作方法
本文實(shí)例為大家分享了Android畫畫板展示的具體代碼,供大家參考,具體內(nèi)容如下
main.xml布局
<RelativeLayout 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"
tools:context="com.example.demo.MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅色"
android:onClick="onplay"
/>
<Button
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="綠色"
android:onClick="onplay"
/>
<Button
android:id="@+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷子"
android:onClick="onplay"
/>
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:onClick="onplay"
/>
<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="涂漆"
android:onClick="onplay"
/>
</LinearLayout>
</RelativeLayout>
main布局
/*
畫板canvas 畫板paint 手勢(shì)識(shí)別器
整體思路:因?yàn)槲沂菆D片是作畫,實(shí)際是對(duì)圖片進(jìn)行修改,起到畫圖的效果
1.原圖,白紙,畫筆,畫板
2.根據(jù)手勢(shì)識(shí)別進(jìn)行作畫
*/
public class MainActivity extends AppCompatActivity {
private Bitmap bitmap;
private Canvas canvas;
private ImageView iv;
private int startx;
private int starty;
private Paint paint;
private Bitmap bmSrc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加載原圖
bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
//創(chuàng)建白紙,寬,高,圖片的參數(shù)
bitmap = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
//創(chuàng)建畫板,參數(shù)是白紙對(duì)象
canvas = new Canvas(bitmap);
//創(chuàng)建畫筆
paint = new Paint();
//在紙上作畫
iv=(ImageView)findViewById(R.id.iv);
canvas.drawBitmap(bmSrc,new Matrix(), paint);
//-----------------手勢(shì)識(shí)別器和畫筆結(jié)合的知識(shí)-------------------
//給控件設(shè)置手勢(shì)適配器,可以得到用戶在這個(gè)控件上所做的手勢(shì)
iv.setOnTouchListener(new View.OnTouchListener() {
//當(dāng)用戶手在這個(gè)控件時(shí),指的就是用戶的手對(duì)控件滑動(dòng),按下,松開的三種場(chǎng)景,自動(dòng)回調(diào)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN://按下時(shí)回調(diào)一次
//獲取用戶手指按下時(shí)的坐標(biāo)
startx = (int) motionEvent.getX();
starty = (int) motionEvent.getY();
break;
case MotionEvent.ACTION_MOVE://手指滑動(dòng)時(shí),不停地調(diào)用
int newx = (int) motionEvent.getX();
int newy = (int) motionEvent.getY();
//在背景圖畫線
canvas.drawLine(startx,starty,newx,newy, paint);
startx=newx;
starty=newy;
iv.setImageBitmap(bitmap);
break;
case MotionEvent.ACTION_UP://松開時(shí)回調(diào)一次
break;
}
//事情分發(fā)機(jī)制
//true:iv處理該觸摸事件
//false:iv不處理該觸摸事件,事件傳遞給上一級(jí)
return true;
}
});
}
public void onplay(View view){
switch (view.getId()){
case R.id.red:
paint.setColor(Color.RED);
break;
case R.id.green:
paint.setColor(Color.GREEN);
break;
case R.id.root:
paint.setStrokeWidth(5);
break;
case R.id.save:
if(SaveViewUtil.saveScreen(iv)){
Toast.makeText(this, "截圖成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "截圖失敗,請(qǐng)檢查sdcard是否可用", Toast.LENGTH_SHORT).show();
}
break;
//涂漆
case R.id.finish:
canvas.drawRect(new Rect(0,0,width,height), paint);
break;
}
}
}
這是一個(gè)把畫的圖存儲(chǔ)SD卡的工具類
public class SaveViewUtil {
private static final File rootDir = new File(Environment.getExternalStorageDirectory()+File.separator);
/**保存截圖的方法*/
public static boolean saveScreen(View view){
//判斷sdcard是否可用
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
return false;
}
if(!rootDir.exists()){
rootDir.mkdir();
}
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
try {
bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(new File(rootDir,System.currentTimeMillis()+".jpg")));
return true;
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}finally{
view.setDrawingCacheEnabled(false);
bitmap = null;
}
}
}
<!-- 往SDCard寫入數(shù)據(jù)權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Build類的詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android Build類的詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,希望通過(guò)本文大家能夠理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08
淺談關(guān)于Android WebView上傳文件的解決方案
這篇文章主要介紹了淺談關(guān)于Android WebView上傳文件的解決方案 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來(lái)電號(hào)碼的方法(基于PhoneStateListene實(shí)現(xiàn))
這篇文章主要介紹了Android監(jiān)聽手機(jī)電話狀態(tài)與發(fā)送郵件通知來(lái)電號(hào)碼的方法,通過(guò)Android的PhoneStateListene實(shí)現(xiàn)該功能,需要的朋友可以參考下2016-01-01
詳解Android實(shí)現(xiàn)定時(shí)器的幾種方法
本篇文章主要介紹了詳解Android實(shí)現(xiàn)定時(shí)器的幾種方法,主要包括了Handler, Timer, Thread, AlarmManager,有興趣的可以了解一下2017-09-09
詳解Android中fragment和viewpager的那點(diǎn)事兒
本文主要對(duì)Android中fragment和viewpager進(jìn)行詳細(xì)介紹,具有一定的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
Android利用LitePal操作數(shù)據(jù)庫(kù)存取圖片
這篇文章主要為大家詳細(xì)介紹了Android利用LitePal操作數(shù)據(jù)庫(kù)存取圖片的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

