国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

實(shí)例講解Android中的View類以及自定義View控件的方法

2020-02-21 17:24:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android開(kāi)發(fā)過(guò)程中,有時(shí)候很多空間都無(wú)法滿足開(kāi)發(fā)項(xiàng)目的需求,這個(gè)時(shí)候我們需要怎么實(shí)現(xiàn)呢?接下來(lái),武林技術(shù)頻道小編就給大家詳解實(shí)例講解Android中的View類以及自定義View控件的方法。

View的簡(jiǎn)單理解和實(shí)例
1.View的基本概念

在Activity顯示的控件 都叫做View(View類 是所有的控件類的父類? 比如 文本 按鈕)

2.在Activity當(dāng)中獲取代表View的對(duì)象
Activity讀取布局文件生成相對(duì)應(yīng)的 各種View對(duì)象

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)聽(tīng)器
一個(gè)控件可以綁定多個(gè)監(jiān)聽(tīng)器 不通過(guò)的監(jiān)聽(tīng)器響應(yīng)不同的事件:

(1)獲取代表控件的對(duì)象
(2)定義一個(gè)類,實(shí)現(xiàn)監(jiān)聽(tīng)接口 implements? OnClickListener
(3)生成監(jiān)聽(tīng)對(duì)象
(4)為控件綁定監(jiān)聽(tīng)對(duì)象

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)聽(tīng)對(duì)象     button.setOnClickListener(buttoneListener);// 按鈕綁定一個(gè)監(jiān)聽(tīng)器   }    @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)建一個(gè)類實(shí)現(xiàn)監(jiān)聽(tīng)事件的接口   {      @Override     public void onClick(View arg0) {       // TODO Auto-generated method stub       count++;       textView.setText(Integer.toString(count));      }    }  } 

View的自定義
通過(guò)繼承View,可以很方便地定制出有個(gè)性的控件出來(lái)。

實(shí)現(xiàn)自定義View的最主要的是重寫(xiě)onDraw(Canvas canvas)函數(shù),當(dāng)每次系統(tǒng)重繪界面的時(shí)候,都會(huì)調(diào)用這個(gè)函數(shù),并傳下一個(gè)Canvas,在這個(gè)函數(shù)內(nèi),應(yīng)該將這個(gè)View所要顯示的內(nèi)容都draw到這個(gè)Canvas上,界面顯示出來(lái)的內(nèi)容幾乎都由這個(gè)Canvas來(lái)決定。Canvas的具體畫(huà)法可以很容易查得到,應(yīng)該說(shuō)Android內(nèi)所有函數(shù)的命名都是很直觀,一目了然的,自己看一下函數(shù)名都大概可以明白這個(gè)函數(shù)是有什么用的。SDK也是查詢Android API的最好的工具,多使用些肯定有好處的。

View的顯示出來(lái)的大小最主要的決定者是Parent Layout,View可以自定義自己的寬高的最小值,但這并不能保證能到達(dá)這種最小值,如果Parent本身的大小已經(jīng)比這個(gè)值小了。

View的重繪——系統(tǒng)不會(huì)經(jīng)常去調(diào)用View的OnDraw函數(shù),為了能夠在View上實(shí)現(xiàn)動(dòng)畫(huà)效果,比如說(shuō)游戲(但好像很多游戲是用更高效的SurfaceView為實(shí)現(xiàn)的),在主線程是執(zhí)行完程序的邏輯后,應(yīng)該要調(diào)用postInvalidate(),通知系統(tǒng)去調(diào)用onDraw函數(shù)去重繪界面,才能將動(dòng)畫(huà)的效果給顯示出來(lái)。

下面的代碼是我自己寫(xiě)的一個(gè)模擬兩個(gè)球不斷碰撞的View,主要由一個(gè)線程來(lái)不斷更新View內(nèi)兩個(gè)球的位置,在發(fā)現(xiàn)兩個(gè)球和墻壁發(fā)生碰撞后,改變球的邏輯參數(shù),更新完后,調(diào)用postInvalidate(),重繪界面。來(lái)實(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退出是時(shí)候,主動(dòng)將線程停止   @Override   public void onDestroy()   {     mScreen.stopDrawing();     super.onDestroy();   } } /**  * 自定義的View類,為兩個(gè)球的碰撞模擬  * @author windy  *  */ class TheScreen extends View {      private static final String TAG = "Draw";   //界面主線程的控制變量   private boolean drawing = false;   //儲(chǔ)存當(dāng)前已有的球的信息   private ArrayList<Circle> circles;   private Paint mPaint;   //兩個(gè)球的運(yùn)動(dòng)范圍   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>();     //加入了兩個(gè)球     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);     //啟動(dòng)界面線程,開(kāi)始自動(dòng)更新界面     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)兩個(gè)球發(fā)生碰撞,交換兩個(gè)球的角度值         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)部類,存儲(chǔ)每一個(gè)球的信息    * @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ā)生碰撞時(shí),改變球的角度     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;     }   } } 

希望本次武林技術(shù)頻道小編給大家分享的實(shí)例講解Android中的View類以及自定義View控件的方法能幫到大家,上述介紹的知識(shí)我們需要認(rèn)真的融會(huì)貫通。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 舒城县| 科技| 定南县| 甘肃省| 抚顺市| 车致| 贵阳市| 山西省| 永兴县| 浮山县| 育儿| 东港市| 滨海县| 建瓯市| 桑日县| 西盟| 井研县| 太康县| 日喀则市| 景泰县| 新巴尔虎左旗| 达拉特旗| 循化| 肇源县| 合作市| 化州市| 于田县| 泰宁县| 沐川县| 嘉禾县| 栾城县| 延庆县| 庄浪县| 电白县| 龙口市| 龙井市| 河源市| 镇远县| 华阴市| 平塘县| 潼南县|