Android實現(xiàn)畫板功能(一)
本文實例為大家分享了Android實現(xiàn)畫板功能的具體代碼,供大家參考,具體內容如下
前言最近看到了一些Android手寫相關的功能,比如說:
釘釘手寫簽名功能,輸入法手寫功能,筆記類App的手寫記錄功能等。最近在工作中也遇到了類似的需求,其實實現(xiàn)畫板功能并不復雜,所以我就打算在這里簡單記錄一下。實現(xiàn)畫板功能比較常用的方法有兩種,一是自定義view的方式在canvas上畫軌跡,另一個是在imageview上畫bitmap。今天就講一下第一種方式吧。
效果圖<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' tools:context='.MainActivity'> <RelativeLayoutandroid:layout_width='match_parent'android:layout_height='55dp'android:background='@color/colorPrimary'><TextView android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='我的畫板' android:layout_marginStart='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:text='清除' android:layout_alignParentEnd='true' android:layout_marginEnd='10dp' android:layout_centerVertical='true' android:textColor='@android:color/white' android:textSize='16sp'/> </RelativeLayout> <com.example.drawline.LineViewandroid: android:layout_marginTop='55dp'android:layout_width='match_parent'android:layout_height='match_parent'> </com.example.drawline.LineView></RelativeLayout>
代碼是用kotlin寫的,但是實現(xiàn)方法和java是一樣的。新建一個自定義view類,繼承自View。kotlin不需要寫View的三個重載方法。只需把三個參數(shù)傳給父類即可。
然后是初始化Paint,Path,設置畫筆顏色等。
關鍵代碼是在onTouchEvent里面,這里需要獲取到手指的位置。在移動手指時調用Path的lineTo(x,y)方法記錄一下軌跡,然后調用invalidate()方法實時更新畫面即可,invalidate()方法會調用onDraw方法,onDraw方法里面調用Canvas的drawPath方法就可以畫出手指劃過的軌跡了。
清除軌跡要調用reset()方法,調用invalidate()方法。
自定義view類package com.example.drawlineimport android.annotation.SuppressLintimport android.content.Contextimport android.graphics.*import android.util.AttributeSetimport android.view.MotionEventimport android.view.Viewclass LineView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { private val defaultPath: Path private val defaultPaint: Paint init {defaultPath = Path()defaultPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)defaultPaint.style = Paint.Style.STROKEdefaultPaint.strokeWidth = 5fdefaultPaint.color = Color.RED } override fun onDraw(canvas: Canvas) {super.onDraw(canvas) canvas.drawPath(defaultPath, defaultPaint) } @SuppressLint('ClickableViewAccessibility') override fun onTouchEvent(event: MotionEvent): Boolean {val x = event.xval y = event.ywhen (event.action) { MotionEvent.ACTION_DOWN -> defaultPath.moveTo(x, y) MotionEvent.ACTION_MOVE -> defaultPath.lineTo(x, y) MotionEvent.ACTION_UP -> defaultPath.lineTo(x, y)}invalidate()return true } fun clear(){defaultPath.reset()invalidate() }}
MainActivity
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)text_clear.setOnClickListener { lineView.clear() } }}
本篇文章中介紹了自定義view的一些基礎知識,適合剛學習自定義view的同學們。后面幾篇文章中將會繼續(xù)深入講解Android自定義view相關知識。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. SpringMVC+Jquery實現(xiàn)Ajax功能2. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉換成文字3. 基于javaweb+jsp實現(xiàn)企業(yè)財務記賬管理系統(tǒng)4. 博客日志摘要暨RSS技術5. 低版本IE正常運行HTML5+CSS3網(wǎng)站的3種解決方案6. vue data有值,但是頁面{{}} 取不到值的解決7. Python中SQLite如何使用8. 在vue項目中利用popstate處理頁面返回的操作介紹9. 解決Vue中使用keepAlive不緩存問題10. Android 解決sqlite無法創(chuàng)建新表的問題
