例1:
界面上每隔100毫秒在随机的位置,以随机的颜色,画一个随机大小的实心圆。
实现方式:handler和timer 的timertask。
public class MyActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
MyView v = new MyView(getApplicationContext());
setContentView(v);
}
private class MyView extends View
{
private Handler mHandler;
private int mColor;
private float cx;
private float <a href="https://www.baidu.com/s?wd=cy&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuyD1rynzujIbPycsnyPW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHf4rHcznHT3" target="_blank" class="baidu-highlight">cy</a>;
private float radius;
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mHandler = new Handler(getMainLooper());
setBackgroundColor(Color.BLACK);
Thread mThread = new Thread(new Runnable() {
@Override
public void run()
{
// TODO Auto-generated method stub
MyView.this.invalidate();
mHandler.postDelayed(this, 100);
}
});
mThread.start();
}
@Override
protected void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);
update();
Paint p = new Paint();
p.setColor(mColor);
canvas.drawCircle(cx, <a href="https://www.baidu.com/s?wd=cy&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuyD1rynzujIbPycsnyPW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHf4rHcznHT3" target="_blank" class="baidu-highlight">cy</a>, radius, p);
}
private void update()
{
Random random = new Random();
cx =(float)(random.nextInt(200)); // 随机生成圆心横坐标(0至200)
<a href="https://www.baidu.com/s?wd=cy&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuyD1rynzujIbPycsnyPW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHf4rHcznHT3" target="_blank" class="baidu-highlight">cy</a> =(float)(random.nextInt(400)); // 随机生成圆心纵坐标(0至400)
radius =(float)( 10 + random.nextInt(90)); // 随机生成圆的半径(10至100)
int r = random.nextInt(256);
int g= random.nextInt(256);
int b = random.nextInt(256);
mColor = Color.rgb(r, g, b); // 随机生成颜色
}
}
}
例2:文字飞入飞出效果/自定义View/随机刷新数据
package com.example.to_games_text;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
* 自定义FramLayout文字飞入飞出效果
* 主页面
* @author Administrator
*
*/
public class SearchFlyActivity extends Activity implements OnClickListener {
public static final String[] keywords = { “QQ”, “BaseAnimation”, “APK”,
“GFW”, “铅笔”,//
“短信”, “桌面精灵”, “MacBook Pro”, “平板电脑”, “雅诗兰黛”,//
“Base”, “笔记本”, “SPY Mouse”, “Thinkpad E40”, “捕鱼达人”,//
“内存清理”, “地图”, “导航”, “闹钟”, “主题”,//
“通讯录”, “播放器”, “CSDN leak”, “安全”, “Animation”,//
“美女”, “天气”, “4743G”, “戴尔”, “联想”,//
“欧朋”, “浏览器”, “愤怒的小鸟”, “mmShow”, “网易公开课”,//
“iciba”, “油水关系”, “网游App”, “互联网”, “365日历”,//
“脸部识别”, “Chrome”, “Safari”, “中国版Siri”, “苹果”,//
“iPhone5S”, “摩托 ME525”, “魅族 MX3”, “小米” };
private KeywordsFlow keywordsFlow;
private Button btnIn, btnOut;
@Override
public void onClick(View v) {
if (v == btnIn) {
keywordsFlow.rubKeywords();
// keywordsFlow.rubAllViews();
feedKeywordsFlow(keywordsFlow, keywords);
keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
} else if (v == btnOut) {
keywordsFlow.rubKeywords();
// keywordsFlow.rubAllViews();
feedKeywordsFlow(keywordsFlow, keywords);
keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
} else if (v instanceof TextView) {
String keyword = ((TextView) v).getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("http://www.google.com.hk/#q=" +
keyword));
startActivity(intent);
Log.e("Search", keyword);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_serch_fly_main);
btnIn = (Button) findViewById(R.id.button1);
btnOut = (Button) findViewById(R.id.button2);
btnIn.setOnClickListener(this);
btnOut.setOnClickListener(this);
keywordsFlow = (KeywordsFlow) findViewById(R.id.frameLayout1);
keywordsFlow.setDuration(800l);
keywordsFlow.setOnItemClickListener(this);
// 添加
feedKeywordsFlow(keywordsFlow, keywords);
keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
}
private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
Random random = new Random();
for (int i = 0; i < KeywordsFlow.MAX; i++) {
int ran = random.nextInt(arr.length);
String tmp = arr[ran];
keywordsFlow.feedKeyword(tmp);
}
}
}