TextView textView=(TextView)findViewBy(R.id.textView)
<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>
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));
}
}
}
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;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有