編輯:關於Android編程
package com.mingrisoft;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private int i = 0; // 記錄其打到了幾只地鼠
private ImageView mouse; // 聲明一個ImageView對象
private Handler handler; // 聲明一個Handler對象
public int[][] position = new int[][] { { 231, 325 }, { 424, 349 },
{ 521, 256 }, { 543, 296 }, { 719, 245 }, { 832, 292 },
{ 772, 358 } }; // 創建一個表示地鼠位置的數組
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mouse = (ImageView) findViewById(R.id.imageView1); // 獲取ImageView對象
mouse.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setVisibility(View.INVISIBLE); // 設置地鼠不顯示
i++;
Toast.makeText(MainActivity.this, 打到[ + i + ]只地鼠!,
Toast.LENGTH_SHORT).show(); // 顯示消息提示框
return false;
}
});
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int index = 0;
if (msg.what == 0x101) {
index = msg.arg1; // 獲取位置索引值
mouse.setX(position[index][0]); // 設置X軸位置
mouse.setY(position[index][1]); // 設置Y軸位置
mouse.setVisibility(View.VISIBLE); // 設置地鼠顯示
}
super.handleMessage(msg);
}
};
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int index = 0; // 創建一個記錄地鼠位置的索引值
while (!Thread.currentThread().isInterrupted()) {
index = new Random().nextInt(position.length); // 產生一個隨機數
Message m = handler.obtainMessage(); // 獲取一個Message
m.what = 0x101; // 設置消息標識
m.arg1 = index; // 保存地鼠標位置的索引值
handler.sendMessage(m); // 發送消息
try {
Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段時間
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start(); // 開啟線程
}
}
<framelayout android:background="@drawable/background" android:id="@+id/fl" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
</framelayout>
Android彈幕框架 黑暗火焰使基本使用方法
今天我將分享由BiliBili開源的Android彈幕框架(DanmakuFlameMaster)的學習經驗。我是將整個框架以model的形式引入項目中的,這樣更方便的觀
Service(服務)
1. 什麼是服務: Android中的四大組件, 是一個可以長時間在後台運行的不提供用戶界面的一個類. 2.服務的特性: 服務一旦被啟動無論啟動它的界面還存不存
Android自定義View實現字母導航欄的代碼
思路分析:1、自定義View實現字母導航欄2、ListView實現聯系人列表3、字母導航欄滑動事件處理4、字母導航欄與中間字母的聯動5、字母導航欄與ListView的聯動
android 顯示gif圖片實例詳解
在android中不支持gif格式的圖片,但是由於我希望在我的程序中剛剛加載的時候有一個小人在跑步表示正在加載。而這個小人跑就是一個gif圖片。也就是希望程序一啟動時就加