實(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的最主要的是重寫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的具體畫法可以很容易查得到,應(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)畫效果,比如說(shuō)游戲(但好像很多游戲是用更高效的SurfaceView為實(shí)現(xiàn)的),在主線程是執(zhí)行完程序的邏輯后,應(yīng)該要調(diào)用postInvalidate(),通知系統(tǒng)去調(diào)用onDraw函數(shù)去重繪界面,才能將動(dòng)畫的效果給顯示出來(lái)。
下面的代碼是我自己寫的一個(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)界面線程,開始自動(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;
}
}
}
這段代碼已經(jīng)寫有注釋了,具體下次再介紹了。。。應(yīng)該不難的看懂的吧。
- Android 自定義View的使用介紹
- Android自定義View實(shí)現(xiàn)廣告信息上下滾動(dòng)效果
- 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)阻尼效果的加載動(dòng)畫
- android開發(fā)教程之實(shí)現(xiàn)listview下拉刷新和上拉刷新效果
- Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)
- Android開發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能
相關(guān)文章
Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二)
我們?cè)趯?shí)際開發(fā)中,有的時(shí)候需要儲(chǔ)存或者備份比較復(fù)雜的數(shù)據(jù)。這些數(shù)據(jù)的特點(diǎn)是,內(nèi)容多、結(jié)構(gòu)大,比如短信備份等,通過(guò)本文給大家介紹Android開發(fā)筆記之Android中數(shù)據(jù)的存儲(chǔ)方式(二),對(duì)android數(shù)據(jù)存儲(chǔ)方式相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
Android實(shí)現(xiàn)列表數(shù)據(jù)按名稱排序、中英文混合排序
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)列表數(shù)據(jù)按名稱排序、中英文混合排序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android popupwindow簡(jiǎn)單使用方法介紹
這篇文章主要為大家詳細(xì)介紹了Android popupwindow簡(jiǎn)單使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android入門之在SharedPreference中使用加密
這篇文章主要為大家詳細(xì)介紹了Android如何使在SharedPreference中使用加密,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12
Android實(shí)現(xiàn)發(fā)送短信功能實(shí)例詳解
這篇文章主要介紹了Android實(shí)現(xiàn)發(fā)送短信功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)發(fā)送短信的原理、步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-02-02
解決在eclipse中將android項(xiàng)目生成apk并且給apk簽名的實(shí)現(xiàn)方法詳解
本篇文章是對(duì)在eclipse中將android項(xiàng)目生成apk并且給apk簽名的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android應(yīng)用開發(fā)中RecyclerView組件使用入門教程
這篇文章主要介紹了Android應(yīng)用開發(fā)中RecyclerView組件使用的入門教程,RecyclerView主要針對(duì)安卓5.0以上的material design開發(fā)提供支持,需要的朋友可以參考下2016-02-02
關(guān)注Ionic底部導(dǎo)航按鈕tabs在android情況下浮在上面的處理
Ionic是一款流行的移動(dòng)端開發(fā)框架,但是剛?cè)腴T的同學(xué)會(huì)發(fā)現(xiàn),Ionic在iOS和Android的底部tabs顯示不一樣。在安卓情況下底部tabs會(huì)浮上去,下面給大家介紹下實(shí)現(xiàn)代碼,一起看看吧2016-12-12

