发布于 2016-05-16 01:13:56 | 151 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要介绍了Android中的View类以及自定义View控件的方法,讲解了如何继承View类并且展示了一个对View进行重绘的例子,需要的朋友可以参考下

View的简单理解和实例
1.View的基本概念
在Activity显示的控件 都叫做View(View类 是所有的控件类的父类  比如 文本 按钮)

2.在Activity当中获取代表View的对象
Activity读取布局文件生成相对应的 各种View对象


TextView textView=(TextView)findViewBy(R.id.textView)

3.设置view的属性
Activity_mian.xml 这样的xml布局文件中发现了,类似@+id/和@id/到底有什么区别呢? 这里@可以理解为引用,而多出的+代表自己新声明的

4.为View设置监听器
一个控件可以绑定多个监听器 不通过的监听器响应不同的事件:

(1)获取代表控件的对象
(2)定义一个类,实现监听接口 implements  OnClickListener
(3)生成监听对象
(4)为控件绑定监听对象

5.实例
布局文件(改成垂直布局)


<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="点击"/> 
 
</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();// 生成监听对象 
    button.setOnClickListener(buttoneListener);// 按钮绑定一个监听器 
  } 
 
  @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// 创建一个类实现监听事件的接口 
  { 
 
    @Override 
    public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      count++; 
      textView.setText(Integer.toString(count)); 
 
    } 
 
  } 
 
} 

View的自定义
通过继承View,可以很方便地定制出有个性的控件出来。

实现自定义View的最主要的是重写onDraw(Canvas canvas)函数,当每次系统重绘界面的时候,都会调用这个函数,并传下一个Canvas,在这个函数内,应该将这个View所要显示的内容都draw到这个Canvas上,界面显示出来的内容几乎都由这个Canvas来决定。Canvas的具体画法可以很容易查得到,应该说Android内所有函数的命名都是很直观,一目了然的,自己看一下函数名都大概可以明白这个函数是有什么用的。SDK也是查询Android API的最好的工具,多使用些肯定有好处的。

View的显示出来的大小最主要的决定者是Parent Layout,View可以自定义自己的宽高的最小值,但这并不能保证能到达这种最小值,如果Parent本身的大小已经比这个值小了。

View的重绘——系统不会经常去调用View的OnDraw函数,为了能够在View上实现动画效果,比如说游戏(但好像很多游戏是用更高效的SurfaceView为实现的),在主线程是执行完程序的逻辑后,应该要调用postInvalidate(),通知系统去调用onDraw函数去重绘界面,才能将动画的效果给显示出来。

下面的代码是我自己写的一个模拟两个球不断碰撞的View,主要由一个线程来不断更新View内两个球的位置,在发现两个球和墙壁发生碰撞后,改变球的逻辑参数,更新完后,调用postInvalidate(),重绘界面。来实现效果


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); 
  } 
   
  //为避免在程序退出后线程仍在进行,造成不必要的系统资源浪费,在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; 
  //储存当前已有的球的信息 
  private ArrayList<Circle> circles; 
  private Paint mPaint; 
  //两个球的运动范围 
  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(); 
          //通知系统更新界面,相当于调用了onDraw函数 
          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); 
    } 
  } 
   
  //界面的逻辑函数,主要检查球是否发生碰撞,以及更新球的位置 
  private void update() 
  { 
    if( circles.size()>1) 
    { 
      for( int i1=0; i1<circles.size()-1; i1++) 
      { 
        //当两个球发生碰撞,交换两个球的角度值 
        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的内部类,存储每一个球的信息 
   * @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; 
    } 
     
    //利用三角函数计算出球的新位置值,当与边界发生碰撞时,改变球的角度 
    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; 
    } 
  } 
} 

这段代码已经写有注释了,具体下次再介绍了。。。应该不难的看懂的吧。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务