C#運用XML作為數據庫的疾速開辟框架完成辦法。本站提示廣大學習愛好者:(C#運用XML作為數據庫的疾速開辟框架完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#運用XML作為數據庫的疾速開辟框架完成辦法正文
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;
}
}
}
這段代碼曾經寫有正文了,詳細下次再引見了。。。應當不難的看懂的吧。