Android studio實(shí)現(xiàn)畫板功能
簡單概述
在日常生活中,我們經(jīng)常會突發(fā)一些奇思妙想,或是一個畫面,或是幾個符號。這時候無法使用拍照或者打字功能實(shí)現(xiàn),想拿筆記下又身邊找不到筆。于是我琢磨能不能做一個手機(jī)端的畫板。
效果圖
實(shí)現(xiàn)過程
項目布局很簡單
讓我們來看代碼:首先聲明畫筆,畫板,和坐標(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è)置當(dāng)前按鈕的文本顏色,最后注意要設(shè)置畫筆寬度,以免在后面點(diǎn)橡皮擦的時候畫筆寬度調(diào)不回來
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)建一張空白圖片和一張灰色畫布,將圖片放在畫布上面
注冊觸摸監(jiān)聽事件,獲取鼠標(biāo)按下時的坐標(biāo)和鼠標(biāo)移動后的坐標(biāo)。在開始和結(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); } });
呃,這里會把畫布擦掉…也就是擦成白色…
最后看看頁面布局
<?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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)筆記之通過API接口將LaTex數(shù)學(xué)函數(shù)表達(dá)式轉(zhuǎn)化為圖片形式的相關(guān)資料,需要的朋友可以參考下2015-11-11Kotlin Channel處理多個數(shù)據(jù)組合的流
最近項目中對 kotlin 的使用比較多。不得不說 kotlin 確實(shí)可以極大的提高 android 的開發(fā)效率,channel用于協(xié)程之間的通訊,使用send和receive往通道里寫入或者讀取數(shù)據(jù),2個方法為非阻塞掛起函數(shù),channel是熱流,不管有沒有訂閱者都會發(fā)送2022-11-11Android Tablayout 自定義Tab布局的使用案例
這篇文章主要介紹了Android Tablayout 自定義Tab布局的使用案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08直接應(yīng)用項目中的Android圖片緩存技術(shù)
這篇文章主要為大家詳細(xì)介紹了直接應(yīng)用項目中的Android圖片緩存技術(shù),簡單、方便、高效,感興趣的小伙伴們可以參考一下2016-04-04EasyValidate優(yōu)雅地校驗提交數(shù)據(jù)完整性
這篇文章主要介紹了EasyValidate優(yōu)雅地校驗提交數(shù)據(jù)完整性,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12Android開發(fā)Jetpack組件LiveData使用講解
LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)2022-08-08總結(jié)Android中多線程更新應(yīng)用的頁面信息的方式
這篇文章主要介紹了總結(jié)Android中多線程更新應(yīng)用的頁面信息的方式,文中共總結(jié)了runOnUiThread、Handler、AsyncTask異步以及View直接在UI線程中更新的方法,需要的朋友可以參考下2016-02-02