日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 自己定义View须要重写ondraw()等方法

發布時間:2023/12/20 Android 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 自己定义View须要重写ondraw()等方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Android ?自己定義View須要重寫ondraw()等方法。這篇博客給大家說說自己定義View的寫法,須要我們繼承View,然后重寫一些

方法,方法多多,看你須要什么方法


首先寫一個自己定義的View 繼承View

package com.example.engineerjspview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; /*** 自己定義 EngineerJspView* @author Engineer-Jsp* @date 2014.10.28* */ import android.view.View; public class EngineerJspView extends View{public float E_X = 188; public float E_Y = 188; Paint paint = new Paint(); public EngineerJspView(Context context) {super(context);}public EngineerJspView(Context context, AttributeSet set){super(context, set); }@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);paint.setColor(Color.RED); canvas.drawCircle(E_X, E_Y, 88, paint);}@Overridepublic boolean onTouchEvent(MotionEvent event) {E_X = event.getX(); E_X = event.getY(); invalidate();return false;}@Overrideprotected void onAnimationStart() {super.onAnimationStart();}@Overrideprotected void onAnimationEnd() {super.onAnimationEnd();}}
布局文件:

<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=".MainActivity" ><com.example.engineerjspview.EngineerJspViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true" /></RelativeLayout>
主活動:

package com.example.engineerjspview; /*** 自己定義 EngineerJspView* @author Engineer-Jsp* @date 2014.10.28 * */ import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
效果:



自己定義View看你需求而定,并不僅僅是繪圖形而已。我這里僅僅是說明下自己定義View須要重寫的方法~~

總結

以上是生活随笔為你收集整理的Android 自己定义View须要重写ondraw()等方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。