Android實現(xiàn)畫板、寫字板功能(附源碼下載)
更新時間:2017年01月24日 14:14:16 投稿:daisy
這篇文章主要介紹了Android實現(xiàn)畫板、寫字板功能的方法,文中給出了簡單的介紹和示例代碼,想要了解更多的朋友可以下載源碼進行學(xué)習(xí),感興趣的朋友們下面來一起看看吧。
前言
本文給大家分享一個使用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應(yīng)用啟動另外一個apk應(yīng)用的方法
這篇文章主要介紹了Android應(yīng)用啟動另外一個apk應(yīng)用的方法,涉及Android基于intent的package調(diào)用與管理技巧,需要的朋友可以參考下2016-02-02Flutter有狀態(tài)組件StatefulWidget生命周期詳解
這篇文章主要為大家介紹了Flutter有狀態(tài)組件StatefulWidget生命周期詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01Android嵌套滾動與協(xié)調(diào)滾動的實現(xiàn)方式匯總
如何實現(xiàn)這種協(xié)調(diào)滾動的布局呢,我們使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior實現(xiàn),另一種方案是MotionLayout,我們看看都是怎么實現(xiàn)的吧2022-06-06利用Warensoft Stock Service編寫高頻交易軟件
本文主要介紹了利用Warensoft Stock Service編寫高頻交易軟件的方法步驟,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01