今天就跟大家聊聊有關(guān)怎么在Android studio項(xiàng)目中實(shí)現(xiàn)一個(gè)畫板功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元廊坊做網(wǎng)站,已為上家服務(wù),為廊坊各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792項(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>
看完上述內(nèi)容,你們對(duì)怎么在Android studio項(xiàng)目中實(shí)現(xiàn)一個(gè)畫板功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站標(biāo)題:怎么在Androidstudio項(xiàng)目中實(shí)現(xiàn)一個(gè)畫板功能-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article8/hepop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站改版、外貿(mào)建站、軟件開(kāi)發(fā)、標(biāo)簽優(yōu)化、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容