Android實現(xiàn)畫板、寫字板功能(附源碼下載)
前言
本文給大家分享一個使用Android開發(fā)寫字板功能Dem、簡單操作內(nèi)存中的圖像、對圖像進行簡單的處理、繪制直線、以達到寫字板的效果
效果圖如下

XML布局代碼
<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="com.tomes.paint.MainActivity" > <ImageView android:id="@ id/iv_drawingBoard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg"/> </RelativeLayout>
Java代碼
public void init() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.bg);
copyBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
paint = new Paint();
canvas = new Canvas(copyBitmap);
Matrix matrix=new Matrix();
canvas.drawBitmap(bitmap, matrix, paint);
imageView = (ImageView) findViewById(R.id.iv_drawingBoard);
imageView.setImageBitmap(copyBitmap);
imageView.setOnTouchListener(new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
startX=event.getX();
startY=event.getY();
break;
case MotionEvent.ACTION_MOVE:
float currentX=event.getX();
float currentY=event.getY();
canvas.drawLine(startX, startY, currentX, currentY, paint);
imageView.setImageBitmap(copyBitmap);
startX=currentX;
startY=currentY;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
});
}
源碼下載:點擊這里
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對各位Android開發(fā)者們能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Android系統(tǒng)進程間通信(IPC)機制Binder中的Server和Client獲得Service Manager接
本文主要介紹Android IPC通信Binder中的Server和Client獲得Service Manager接口,這里詳細的說明了如何實現(xiàn)Service Manager接口,對研究Android源碼的朋友提供幫助,有需要的小伙伴可以參考下2016-08-08
Android開發(fā)筆記之:Splash的實現(xiàn)詳解
本篇文章是對Android中Splash的實現(xiàn)進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Android開發(fā)Kotlin語言協(xié)程中的并發(fā)問題和互斥鎖
Android開發(fā)Kotlin語言提供了多種機制來處理并發(fā)和同步,其中包括高層次和低層次的工具,對于常規(guī)的并發(fā)任務(wù),可以利用 Kotlin 協(xié)程提供的結(jié)構(gòu)化并發(fā)方式,而對于需要更低層次的鎖定機制,可以使用Mutex(互斥鎖)來實現(xiàn)對共享資源的線程安全訪問2024-06-06
Android中使用ViewStub實現(xiàn)布局優(yōu)化
ViewStub是Android布局優(yōu)化中一個很不錯的標(biāo)簽/控件,直接繼承自View。雖然Android開發(fā)人員基本上都聽說過,但是真正用的可能不多。今天我們就來詳細探討下ViewStub的使用2016-09-09
Android ViewPager實現(xiàn)圖片輪翻效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)圖片輪翻效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Android實現(xiàn)可輸入數(shù)據(jù)的彈出框
這篇文章主要為大家詳細介紹了Android實現(xiàn)可輸入數(shù)據(jù)的彈出框,文章提供了兩種方式,示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android中ListView下拉刷新的實現(xiàn)方法實例分析
這篇文章主要介紹了Android中ListView下拉刷新的實現(xiàn)方法,涉及Android操作ListView的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10

