Android studio實(shí)現(xiàn)畫板功能
簡(jiǎn)單概述
在日常生活中,我們經(jīng)常會(huì)突發(fā)一些奇思妙想,或是一個(gè)畫面,或是幾個(gè)符號(hào)。這時(shí)候無(wú)法使用拍照或者打字功能實(shí)現(xiàn),想拿筆記下又身邊找不到筆。于是我琢磨能不能做一個(gè)手機(jī)端的畫板。
效果圖

實(shí)現(xiàn)過(guò)程
項(xiàng)目布局很簡(jiǎn)單

讓我們來(lái)看代碼:首先聲明畫筆,畫板,和坐標(biāo)
public class MainActivity extends AppCompatActivity{
Paint paint;
Canvas canvas;
ImageView imageview;
Bitmap bitmap,newbitmap;
TextView tv_stroke;
int startX, startY, endX, endY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_paint_tools);
LinearLayout ll_layout = findViewById(R.id.ll_layout);
RadioGroup rg_color = findViewById(R.id.rg_color);
遍歷單選按鈕,當(dāng)單選按鈕選中時(shí),獲取單選按鈕顏色并將畫筆顏色設(shè)置當(dāng)前按鈕的文本顏色,最后注意要設(shè)置畫筆寬度,以免在后面點(diǎn)橡皮擦的時(shí)候畫筆寬度調(diào)不回來(lái)
for (int i = 0;i<rg_color.getChildCount();i++){
RadioButton rb = (RadioButton) rg_color.getChildAt(i);
rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isChecked()){
paint.setColor(buttonView.getTextColors().getDefaultColor());
paint.setStrokeWidth(5);
}
}
});
}
首先創(chuàng)建一張空白圖片和一張灰色畫布,將圖片放在畫布上面
注冊(cè)觸摸監(jiān)聽(tīng)事件,獲取鼠標(biāo)按下時(shí)的坐標(biāo)和鼠標(biāo)移動(dòng)后的坐標(biāo)。在開(kāi)始和結(jié)束之間畫一條直線并更新畫布圖片
imageview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.i("MyPaintToolsActivity","ACTION_DOWN");
startX = (int) (event.getX()/1.4);
startY = (int) (event.getY()/1.4);
break;
case MotionEvent.ACTION_MOVE:
Log.i("MyPaintToolsActivity","ACTION_MOVE");
endX = (int) (event.getX()/1.4);
endY = (int) (event.getY()/1.4);
canvas.drawLine(startX,startY,endX,endY,paint);
startX = (int) (event.getX()/1.4);
startY = (int) (event.getY()/1.4);
imageview.setImageBitmap(bitmap);
break;
case MotionEvent.ACTION_UP:
Log.i("MyPaintToolsActivity","ACTION_UP");
break;
}
imageview.invalidate();
return true;
}
});
清屏的話就一行代碼 ,剩下的是重新生成一塊畫布
Button btn_clear = findViewById(R.id.btn_clear);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
canvas.drawColor(0,PorterDuff.Mode.CLEAR);
bitmap = Bitmap.createBitmap(888,1200,Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
canvas.drawColor(Color.argb(100,0,0,0));
paint = new Paint();
paint.setStrokeWidth(5);
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawBitmap(bitmap,new Matrix(),paint);
imageview.setImageBitmap(bitmap);
}
});
呃,這里會(huì)把畫布擦掉…也就是擦成白色…
最后看看頁(yè)面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/ll_layout"> <!-- tools:context=".MyPaintToolsActivity">--> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <RadioGroup android:background="#747373" android:layout_width="match_parent" android:orientation="horizontal" android:id="@+id/rg_color" android:layout_height="wrap_content"> <RadioButton android:id="@+id/rb_red" android:layout_width="wrap_content" android:layout_height="43dp" android:layout_weight="1" android:text="紅色" android:textColor="#FF0000" android:textSize="18sp" /> <RadioButton android:id="@+id/rb_green" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_weight="1" android:text="黑色" android:textColor="#000000" android:textSize="18sp" /> <RadioButton android:id="@+id/rb_blue" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_weight="1" android:text="白色" android:textColor="#FFFFFF" android:textSize="18sp" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btn_clear" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:background="#000000" android:textColor="#FFFFFF" android:textSize="18sp" android:text="清除"/> <Button android:id="@+id/btn_eraser" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:textColor="#FFFFFF" android:textSize="18sp" android:background="#000000" android:text="擦除"/> </LinearLayout> </LinearLayout>
到此這篇關(guān)于Android studio實(shí)現(xiàn)畫板功能的文章就介紹到這了,更多相關(guān)Android studio畫板功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)畫板功能(二)
- Android實(shí)現(xiàn)畫板功能(一)
- Android實(shí)現(xiàn)簡(jiǎn)單畫圖畫板
- Android SurfaceView畫板操作
- Android實(shí)現(xiàn)畫畫板案例
- Android畫板開(kāi)發(fā)之添加文本文字
- Android畫板開(kāi)發(fā)之添加背景和保存畫板內(nèi)容為圖片
- Android畫板開(kāi)發(fā)之撤銷反撤銷功能
- Android畫板開(kāi)發(fā)之基本畫筆功能
- Android畫板開(kāi)發(fā)之橡皮擦功能
- Android曲線更圓滑的簽名畫板
- Android實(shí)現(xiàn)繪畫板功能
相關(guān)文章
Android程序開(kāi)發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)
這篇文章主要介紹了Android程序開(kāi)發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
解決Android Studio 代碼自動(dòng)提示突然失效的問(wèn)題
這篇文章主要介紹了解決Android Studio 代碼自動(dòng)提示突然失效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
這篇文章主要介紹了Android中AlertDialog各種對(duì)話框的用法在項(xiàng)目開(kāi)發(fā)中經(jīng)常用的到,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值2016-04-04
Android apk 項(xiàng)目一鍵打包并上傳到蒲公英的實(shí)現(xiàn)方法
這篇文章主要介紹了Android apk 項(xiàng)目一鍵打包并上傳到蒲公英,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法
下面小編就為大家分享一篇Android 實(shí)現(xiàn)背景圖和狀態(tài)欄融合方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問(wèn)題
這篇文章主要介紹了Android獲取清單文件中的meta-data,解決碰到數(shù)值為null的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
flutter優(yōu)雅實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源示例詳解
這篇文章主要為大家介紹了flutter優(yōu)雅實(shí)現(xiàn)掃碼槍獲取數(shù)據(jù)源示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

