在Activiey 中更改 并刷新Layout(布局)中的控件(ImageView)的显示

/*

里面有自定义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;
//当前操作的门
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);
//获取当前操作的门
getThisDoor();
// 设置门开始界面
setDoorFace(); // dooropen 打开;doopclose 为关闭

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("监听到将要 打开门");
if (!Values.doorIsOpen[thisWindow]) {
// 更新门状态
updateDoorState(true);
linearLayoutDoor.removeAllViews();
linearLayoutDoor.addView(doorOperationView);
}else {
toast = Toast.makeText(getApplicationContext(),"门已经打开了!", 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("监听到将要 关闭门");
if (Values.doorIsOpen[thisWindow]) {
// 更新门状态
updateDoorState(false);
linearLayoutDoor.removeAllViews();
linearLayoutDoor.addView(doorOperationView);
}else {
toast = Toast.makeText(getApplicationContext(),
"门已经关上了!", 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("监听到确定按钮");
if (doorOperationView != null) {
finish();
}
}
});*/

}
public void updateDoorState(boolean blean) {
Values.doorIsOpen[thisWindow]=blean;
doorOperationView =new DoorOperationView(DoorActivity.this,Values.doorIsOpen[thisWindow]);
}

//获取当前操作的门
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("门已关闭");
};
return thisWindow;
}
// 设置门开始界面
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){//窗户为打开状态 这里是关闭动画
bitmap = ((BitmapDrawable)doorActivity.getResources().getDrawable(R.drawable.dooropen)).getBitmap();
}else if (!isOpen) {//窗户为关闭状态 这里是打开动画
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;//当前操作的控件的ID

//对应七扇门,是否打开 true 表示门是打开的 false 表示门是关闭的
public static boolean doorIsOpen[]={
true, //门1
true, //门2
true, //门3
true, //门4
true, //门5
true, //门6
true, //门7
};


}

//点击打开按钮时

//点击关闭按钮时

//源码下载

http://download.csdn.net/source/3222583

如果没有资源分的或没有CSDN号的可以留邮箱,或发邮件至254443233@qq.com;本人免费放送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值