實(shí)例講解Android中的View類以及自定義View控件的方法
View的簡單理解和實(shí)例
1.View的基本概念
在Activity顯示的控件 都叫做View(View類 是所有的控件類的父類 比如 文本 按鈕)
2.在Activity當(dāng)中獲取代表View的對象
Activity讀取布局文件生成相對應(yīng)的 各種View對象
TextView textView=(TextView)findViewBy(R.id.textView)
3.設(shè)置view的屬性
Activity_mian.xml 這樣的xml布局文件中發(fā)現(xiàn)了,類似@+id/和@id/到底有什么區(qū)別呢? 這里@可以理解為引用,而多出的+代表自己新聲明的
4.為View設(shè)置監(jiān)聽器
一個控件可以綁定多個監(jiān)聽器 不通過的監(jiān)聽器響應(yīng)不同的事件:
(1)獲取代表控件的對象
(2)定義一個類,實(shí)現(xiàn)監(jiān)聽接口 implements OnClickListener
(3)生成監(jiān)聽對象
(4)為控件綁定監(jiān)聽對象
5.實(shí)例
布局文件(改成垂直布局)
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="80px" android:background="#FF0000" android:text="hello_world 熊" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="點(diǎn)擊"/> </LinearLayout>
MianActivity文件
package com.xiong.fisrt_android; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView; private Button button; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); button = (Button) findViewById(R.id.button); textView.setText("hello Android!!!"); textView.setBackgroundColor(Color.BLUE); ButtoneListener buttoneListener = new ButtoneListener();// 生成監(jiān)聽對象 button.setOnClickListener(buttoneListener);// 按鈕綁定一個監(jiān)聽器 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class ButtoneListener implements OnClickListener// 創(chuàng)建一個類實(shí)現(xiàn)監(jiān)聽事件的接口 { @Override public void onClick(View arg0) { // TODO Auto-generated method stub count++; textView.setText(Integer.toString(count)); } } }
View的自定義
通過繼承View,可以很方便地定制出有個性的控件出來。
實(shí)現(xiàn)自定義View的最主要的是重寫onDraw(Canvas canvas)函數(shù),當(dāng)每次系統(tǒng)重繪界面的時候,都會調(diào)用這個函數(shù),并傳下一個Canvas,在這個函數(shù)內(nèi),應(yīng)該將這個View所要顯示的內(nèi)容都draw到這個Canvas上,界面顯示出來的內(nèi)容幾乎都由這個Canvas來決定。Canvas的具體畫法可以很容易查得到,應(yīng)該說Android內(nèi)所有函數(shù)的命名都是很直觀,一目了然的,自己看一下函數(shù)名都大概可以明白這個函數(shù)是有什么用的。SDK也是查詢Android API的最好的工具,多使用些肯定有好處的。
View的顯示出來的大小最主要的決定者是Parent Layout,View可以自定義自己的寬高的最小值,但這并不能保證能到達(dá)這種最小值,如果Parent本身的大小已經(jīng)比這個值小了。
View的重繪——系統(tǒng)不會經(jīng)常去調(diào)用View的OnDraw函數(shù),為了能夠在View上實(shí)現(xiàn)動畫效果,比如說游戲(但好像很多游戲是用更高效的SurfaceView為實(shí)現(xiàn)的),在主線程是執(zhí)行完程序的邏輯后,應(yīng)該要調(diào)用postInvalidate(),通知系統(tǒng)去調(diào)用onDraw函數(shù)去重繪界面,才能將動畫的效果給顯示出來。
下面的代碼是我自己寫的一個模擬兩個球不斷碰撞的View,主要由一個線程來不斷更新View內(nèi)兩個球的位置,在發(fā)現(xiàn)兩個球和墻壁發(fā)生碰撞后,改變球的邏輯參數(shù),更新完后,調(diào)用postInvalidate(),重繪界面。來實(shí)現(xiàn)效果
package com.androidclub.elfman.homework3; import java.util.ArrayList; import java.util.Random; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.os.Bundle; import android.view.View; public class Main extends Activity { TheScreen mScreen; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //mScreen是自定義的View mScreen = new TheScreen(this); setContentView(mScreen); } //為避免在程序退出后線程仍在進(jìn)行,造成不必要的系統(tǒng)資源浪費(fèi),在Activity退出是時候,主動將線程停止 @Override public void onDestroy() { mScreen.stopDrawing(); super.onDestroy(); } } /** * 自定義的View類,為兩個球的碰撞模擬 * @author windy * */ class TheScreen extends View { private static final String TAG = "Draw"; //界面主線程的控制變量 private boolean drawing = false; //儲存當(dāng)前已有的球的信息 private ArrayList<Circle> circles; private Paint mPaint; //兩個球的運(yùn)動范圍 public static final int WIDTH = 300; public static final int HEIGHT = 400; public static final double PI = 3.14159265; Paint mPaint2 = new Paint(); public TheScreen(Context context) { super(context); circles = new ArrayList<Circle>(); //加入了兩個球 circles.add(new Circle()); circles.add(new Circle(20, 30, 10)); mPaint = new Paint(); mPaint.setColor(Color.YELLOW); mPaint.setAntiAlias(true); mPaint2.setStyle(Style.STROKE); mPaint2.setColor(Color.RED); mPaint2.setAntiAlias(true); //啟動界面線程,開始自動更新界面 drawing = true; new Thread(mRunnable).start(); } private Runnable mRunnable = new Runnable() { //界面的主線程 @Override public void run() { while( drawing ) { try { //更新球的位置信息 update(); //通知系統(tǒng)更新界面,相當(dāng)于調(diào)用了onDraw函數(shù) postInvalidate(); //界面更新的頻率,這里是每30ms更新一次界面 Thread.sleep(30); //Log.e(TAG, "drawing"); } catch (InterruptedException e) { e.printStackTrace(); } } } }; public void stopDrawing() { drawing = false; } @Override public void onDraw(Canvas canvas) { //在canvas上繪上邊框 canvas.drawRect(0, 0, WIDTH, HEIGHT, mPaint2); //在canvas上繪上球 for( Circle circle : circles) { canvas.drawCircle(circle.x, circle.y, circle.radius, mPaint); } } //界面的邏輯函數(shù),主要檢查球是否發(fā)生碰撞,以及更新球的位置 private void update() { if( circles.size()>1) { for( int i1=0; i1<circles.size()-1; i1++) { //當(dāng)兩個球發(fā)生碰撞,交換兩個球的角度值 for( int i2=i1+1; i2<circles.size(); i2++) if( checkBumb(circles.get(i1),circles.get(i2))) { circles.get(i1).changeDerection(circles.get(i2)); } } } //更新球的位置 for( Circle circle: circles) circle.updateLocate(); } private boolean checkBumb(Circle c1, Circle c2) { return (c1.x-c2.x)*(c1.x-c2.x) + (c1.y-c2.y)*(c1.y-c2.y) <= (c1.radius+c2.radius)*(c1.radius+c2.radius); } /** * 自定義的View的內(nèi)部類,存儲每一個球的信息 * @author windy * */ class Circle { float x=50; float y=70; double angle= (new Random().nextFloat())*2*PI;; int speed=4; int radius=10; public Circle() { } public Circle( float x, float y, int r ) { this.x = x; this.y = y; radius = r; } //利用三角函數(shù)計(jì)算出球的新位置值,當(dāng)與邊界發(fā)生碰撞時,改變球的角度 public void updateLocate() { x = x+ (float)(speed *Math.cos(angle)); //Log.v(TAG, Math.cos(angle)+""); y = y+ (float)(speed *Math.sin(angle)); //Log.v(TAG, Math.cos(angle)+""); if( (x+radius)>=WIDTH ) { if( angle >=0 && angle <= (PI/2)) angle = PI - angle; if( angle > 1.5 * PI && angle <= 2*PI) angle = 3 * PI - angle; } if( x-radius <=0 ) { if( angle >= PI && angle <= 1.5*PI ) angle = 3*PI - angle; if( angle >= PI/2 && angle < PI) angle = PI - angle; } if( y-radius<=0 || y+radius>=HEIGHT) angle = 2*PI - angle; } //兩球交換角度 public void changeDerection(Circle other) { double temp = this.angle; this.angle = other.angle; other.angle = temp; } } }
這段代碼已經(jīng)寫有注釋了,具體下次再介紹了。。。應(yīng)該不難的看懂的吧。
- Android 自定義View的使用介紹
- Android自定義View實(shí)現(xiàn)廣告信息上下滾動效果
- Android自定義View實(shí)現(xiàn)折線圖效果
- Android自定義View實(shí)現(xiàn)帶數(shù)字的進(jìn)度條實(shí)例代碼
- Android自定義View實(shí)現(xiàn)豎直跑馬燈效果案例解析
- Android自定義view制作絢麗的驗(yàn)證碼
- Android自定義View之繼承TextView繪制背景
- Android自定義view實(shí)現(xiàn)阻尼效果的加載動畫
- android開發(fā)教程之實(shí)現(xiàn)listview下拉刷新和上拉刷新效果
- Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)
- Android開發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能
相關(guān)文章
Android自定義View實(shí)現(xiàn)左右滑動選擇出生年份
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)左右滑動選擇出生年份,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06Android Compose學(xué)習(xí)之繪制速度表盤
這篇文章主要為大家詳細(xì)介紹了Android Compose繪制速度表盤的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解下2023-11-11Android自定義網(wǎng)絡(luò)連接工具類HttpUtil
這篇文章主要介紹了Android自定義網(wǎng)絡(luò)連接工具類HttpUtil,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11android 獲取文件的擴(kuò)展名和去掉文件擴(kuò)展名的小例子
android 獲取文件的擴(kuò)展名和去掉文件擴(kuò)展名的小例子,需要的朋友可以參考一下2013-06-06Android Studio報(bào)錯unable to access android sdk add-on list解決方案
這篇文章主要介紹了Android Studio報(bào)錯unable to access android sdk add-on list解決方案,本文通過多種方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android開發(fā)之無痕過渡下拉刷新控件的實(shí)現(xiàn)思路詳解
下拉刷新效果功能在程序開發(fā)中經(jīng)常會見到,今天小編抽時間給大家分享Android開發(fā)之無痕過渡下拉刷新控件的實(shí)現(xiàn)思路詳解,需要的朋友參考下吧2016-11-11JSON中optString和getString方法的區(qū)別
optString方法會在對應(yīng)的key中的值不存在的時候返回一個空字符串,但是getString會拋一個JSONException 。下面通過一段代碼給大家介紹JSON中optString和getString方法的區(qū)別,感興趣的朋友一起看看吧2017-07-07簡單實(shí)現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地
這篇文章主要為大家詳細(xì)介紹了如何簡單實(shí)現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地的方法,感興趣的小伙伴們可以參考一下2016-08-08Android為Tiny4412設(shè)備驅(qū)動在proc目錄下添加一個可讀版本信息的文件
今天小編就為大家分享一篇關(guān)于Android為Tiny4412設(shè)備驅(qū)動在proc目錄下添加一個可讀版本信息的文件,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12快速關(guān)閉android studio的自動保存功能教程
這篇文章主要介紹了快速關(guān)閉android studio的自動保存功能教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04