Java 如何从setImageResource()调整第二个图像的大小
我是java/android新手,正在制作一个测试应用程序。它有ImageButtons,单击后可以临时切换到不同的图像。原稿是用原稿剪成的Java 如何从setImageResource()调整第二个图像的大小,java,android,eclipse,Java,Android,Eclipse,我是java/android新手,正在制作一个测试应用程序。它有ImageButtons,单击后可以临时切换到不同的图像。原稿是用原稿剪成的 android:adjustViewBounds="true" android:scaleType="centerCrop" 在activity_main.xml中 问题是第二个图像没有被裁剪,因此对于按钮来说太大了。有人知道我怎么解决这个问题吗?以下是其中一个按钮的示例: public void onClick(View v) { /
android:adjustViewBounds="true"
android:scaleType="centerCrop"
在activity_main.xml中
问题是第二个图像没有被裁剪,因此对于按钮来说太大了。有人知道我怎么解决这个问题吗?以下是其中一个按钮的示例:
public void onClick(View v) {
//switch to second img
butt2.setImageResource(R.drawable.newimg);
//switch back to first after pause
new Handler().postDelayed(new Runnable() {
public void run() {
butt2.setImageResource(R.drawable.orig);
}
}, 500L);
}
});
我使用了以下方法
int normal[] = { R.drawable.num0, R.drawable.num1, R.drawable.num2,
R.drawable.num3, R.drawable.num4, R.drawable.num5,
R.drawable.num6, R.drawable.num7, R.drawable.num8,
R.drawable.num9, R.drawable.del, R.drawable.go };
int pressed[] = { R.drawable.num0_clicked, R.drawable.num1_clicked,
R.drawable.num2_clicked, R.drawable.num3_clicked,
R.drawable.num4_clicked, R.drawable.num5_clicked,
R.drawable.num6_clicked, R.drawable.num7_clicked,
R.drawable.num8_clicked, R.drawable.num9_clicked,
R.drawable.del_clicked, R.drawable.go_clicked };
StateListDrawable sld;
for (int i = 0; i < 12; i++) {
sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed },
getResources().getDrawable(pressed[i]));
sld.addState(new int[] {}, getResources().getDrawable(normal[i]));
btns[i].setImageDrawable(sld);
}
int normal[]={R.drawable.num0,R.drawable.num1,R.drawable.num2,
R.drawable.num3,R.drawable.num4,R.drawable.num5,
R.drawable.num6,R.drawable.num7,R.drawable.num8,
R.drawable.num9,R.drawable.del,R.drawable.go};
按int[]={R.drawable.num0_单击,R.drawable.num1_单击,
R.drawable.num2\u单击,R.drawable.num3\u单击,
R.drawable.num4_单击,R.drawable.num5_单击,
R.drawable.num6_单击,R.drawable.num7_单击,
R.drawable.num8_单击,R.drawable.num9_单击,
R.drawable.del_单击,R.drawable.go_单击};
状态列表可绘制sld;
对于(int i=0;i<12;i++){
sld=新StateListDrawable();
sld.addState(新int[]{android.R.attr.state_pressed},
getResources().getDrawable(按[i]);
addState(newint[]{},getResources().getDrawable(normal[i]);
BTN[i].可设置图像绘制(sld);
}
还有另一种尝试方法,如果你想,比如当你点击按钮时,背景应该改变,当你释放它时,背景会变为上一个
这可以通过以下方式实现
现在,在要显示为活动的main.xml文件中,在声明按钮标记的位置添加一条语句
android:background=“@drawable/customDefaultBackground”
这就像
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/customDefaultBackground" />
现在添加选择器标记类型的xml文件并将其命名为customDefaultBackground.xml,打开该文件并将以下内容添加到其内容中:-
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/onPressBackground" ></item>
<item android:drawable="@drawable/defaultBackground"></item>
</selector>
android:state_pressed设置为true的项目标签,表示当用户单击按钮并保持按下时,将显示backgroundOnPressed版本,否则将显示默认背景。是否可能重复?听起来您应该使用选择器(即,
StateListDrawable
),而不是在单击事件上手动切换图像。是否需要为每个按钮单独设置一个xml文件?如果它们有不同的图像,那么每个按钮都应该有不同的XML,因为要为每个按钮设置不同的背景。如果希望每个按钮具有相同的背景,可以使用单个xml。