/*
里面有自定義Toast的顯示位置
*/
//DoorActivity
package temp.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class DoorActivity extends Activity {
// private LayoutInflater inflater = null;
private ImageView doorImageView;
//當(dāng)前操作的門
private int thisWindow=1;
private Button okDoorButton;
private Button openDoorButton;
private Button closeDoorButton;
private Toast toast;
private DoorOperationView doorOperationView;
private LinearLayout linearLayoutDoor;
@Override
public void onCreate(Bundle savedInstanceState) {
//inflater = LayoutInflater.from(this);
super.onCreate(savedInstanceState);
//Thread.currentThread().start();
setContentView(R.layout.doorview);
linearLayoutDoor = (LinearLayout) findViewById(R.id.linearLayoutDoor);
// 獲取門控件
doorImageView = (ImageView) findViewById(R.id.doorImageView);
//獲取當(dāng)前操作的門
getThisDoor();
// 設(shè)置門開始界面
setDoorFace(); // dooropen 打開;doopclose 為關(guān)閉
doorOperationView =new DoorOperationView(DoorActivity.this,Values.doorIsOpen[thisWindow]);
openDoorButton = (Button) DoorActivity.this.findViewById(R.id.openDoorButton);
openDoorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("監(jiān)聽到將要 打開門");
if (!Values.doorIsOpen[thisWindow]) {
// 更新門狀態(tài)
updateDoorState(true);
linearLayoutDoor.removeAllViews();
linearLayoutDoor.addView(doorOperationView);
}else {
toast = Toast.makeText(getApplicationContext(),"門已經(jīng)打開了!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(400);
toast.show();
}
}
});
closeDoorButton = (Button) DoorActivity.this.findViewById(R.id.closeDoorButton);
closeDoorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("監(jiān)聽到將要 關(guān)閉門");
if (Values.doorIsOpen[thisWindow]) {
// 更新門狀態(tài)
updateDoorState(false);
linearLayoutDoor.removeAllViews();
linearLayoutDoor.addView(doorOperationView);
}else {
toast = Toast.makeText(getApplicationContext(),
"門已經(jīng)關(guān)上了!", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(400);
toast.show();
}
}
});
/* okDoorButton = (Button) DoorActivity.this.findViewById(R.id.okDoorButton);
okDoorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("監(jiān)聽到確定按鈕");
if (doorOperationView != null) {
finish();
}
}
});*/
}
public void updateDoorState(boolean blean) {
Values.doorIsOpen[thisWindow]=blean;
doorOperationView =new DoorOperationView(DoorActivity.this,Values.doorIsOpen[thisWindow]);
}
//獲取當(dāng)前操作的門
public int getThisDoor(){
thisWindow=Values.thisWindow-1;
System.out.println("This Door is "+thisWindow);
if(Values.doorIsOpen[thisWindow])
System.out.println("門已打開");
else {
System.out.println("門已關(guān)閉");
};
return thisWindow;
}
// 設(shè)置門開始界面
public void setDoorFace(){
if(Values.doorIsOpen[thisWindow])
doorImageView.setImageResource(R.drawable.dooropen);
else {
doorImageView.setImageResource(R.drawable.doorclose);
}
}
}
//DoorOperationView
package temp.com;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
public class DoorOperationView extends View{
Context mContext = null;
Bitmap bitmap=null;
/* 定義一個Drawable對象 */
Drawable mBitDrawable = null;
boolean isOpen=false;
private Paint paintNormal = null;// 小畫筆
public DoorOperationView(DoorActivity doorActivity,boolean isOpen)
{
super(doorActivity);
this.isOpen=isOpen;
paintNormal = new Paint();
paintNormal.setAntiAlias(true);
paintNormal.setStyle(Paint.Style.STROKE);
paintNormal.setColor(Color.BLACK);
paintNormal.setStrokeWidth(1);
if(isOpen){//窗戶為打開狀態(tài) 這里是關(guān)閉動畫
bitmap = ((BitmapDrawable)doorActivity.getResources().getDrawable(R.drawable.dooropen)).getBitmap();
}else if (!isOpen) {//窗戶為關(guān)閉狀態(tài) 這里是打開動畫
bitmap = ((BitmapDrawable)doorActivity.getResources().getDrawable(R.drawable.doorclose)).getBitmap();
}
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawBitmap(bitmap, 0, 0, paintNormal);
}
}
//Values
package temp.com;
public class Values {
public static int thisWindow=1;//當(dāng)前操作的控件的ID
//對應(yīng)七扇門,是否打開 true 表示門是打開的 false 表示門是關(guān)閉的
public static boolean doorIsOpen[]={
true, //門1
true, //門2
true, //門3
true, //門4
true, //門5
true, //門6
true, //門7
};
}
//點(diǎn)擊打開按鈕時
//點(diǎn)擊關(guān)閉按鈕時
//源碼下載
http://download.csdn.net/source/3222583
如果沒有資源分的或沒有CSDN號的可以留郵箱,或發(fā)郵件至254443233@qq.com;本人免費(fèi)放送
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
