我有一个包含大量图像按钮的滚动视图。我想在按下时更改图像按钮的图像。问题是我希望图像保持不变,直到按下另一个图像按钮。这就是我无法使用选择器的原因。实现他的最佳实践是什么?
最好的问候
答案 0 :(得分:33)
你想这样做。
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
}
}
试试这个。 (更新的setset设置)
答案 1 :(得分:21)
更好的解决方案,使用以下xml作为图像源:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true">
<bitmap android:src="@drawable/image_selected"/>
</item>
<item>
<bitmap android:src="@drawable/image_not_selected"/>
</item>
</selector>
@Override
public void onClick(View v) {
v.setActivated(!v.isActivated());
}
答案 2 :(得分:11)
将以下内容保存为两张图片旁边的drawable/image_pressable.xml
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/image_regular" />
</selector>
然后将其引用为:
<ImageView android:src="@drawable/image_pressable" />
提示:
使用state_pressed
代替state_activated
可以在用手指按压图像时更改图像。 state_activated
可能很有用,如果你想动态改变ImageView的状态,但是如果你只是希望它在按下时表现不同,而不涉及任何代码。这正是本主题提出的问题。
答案 3 :(得分:5)
OnTouchListener对于您必须做的事情要好得多:
myImageButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
myImageButton.setImageResource(R.drawable.image_when_pressed);
break;
case MotionEvent.ACTION_UP :
myImageButton.setImageResource(R.drawable.image_when_released);
break;
}
return false;
}
});
答案 4 :(得分:5)
尝试使用此功能更改图像。按 imageview 时
Like_btn.setOnClickListener(new OnClickListener()
{
**private boolean fun = true;**
public void onClick(View v)
{
if(fun)
{
Like_btn.setImageResource(R.drawable.unlike);
fun=false;
}
else
{
fun=true;
Like_btn.setImageResource(R.drawable.like);
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_LONG).show();
}
}
});
答案 5 :(得分:4)
ImageButton Demo_button = (ImageButton)findViewById(R.id.firstimage);
ImageButton second_button = (ImageButton)findViewById(R.id.secondimage);
// when you click this demo button
Demo_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.secondimage);
second_button.setImageResource(R.drawable.firstimage);
}
}
second_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Demo_button.setImageResource(R.drawable.firstimage);
second_button.setImageResource(R.drawable.secondimage);
}
}
我希望你想要那样 右???
答案 6 :(得分:4)
尝试以下代码: -
boolean flag=false;
ImageButton btn = (ImageButton)findViewById(R.id.btn);
// when you click this demo button
btn .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (!flag) {
btn.setBackgroundResource(R.drawable.imageonpress);
flag=true;
}
else {
btn.setBackgroundResource(R.drawable.image);
flag=false;
}
}
}
答案 7 :(得分:2)
float alpha_first = 0.2f;
float alpha_second = 1.0f;
AlphaAnimation alphadp = new AlphaAnimation(alpha_second, alpha_first);
switch (v.getId()) {
case R.id.disable_deactivate_pic:
ImageButton disable_button =(ImageButton)findViewById(R.id.disable_deactivate_pic);
if (!flag) {
disable_button.setImageResource(R.drawable.enable_active);
linearLayout_for_picture = (LinearLayout) findViewById(R.id.linearlayout_imageView_pic);
alphadp.setFillAfter(true);
linearLayout_for_picture.startAnimation(alphadp);
flag=true;
}
else {
disable_button.setImageResource(R.drawable.disable_active);
alphadp.setFillAfter(false);
linearLayout_for_picture.startAnimation(alphadp);
flag=false;
}
break;
答案 8 :(得分:1)
state_pressed
或state_activated
对我不起作用。
但是,我成功完成了state_enabled
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/checkbox_on" />
<item android:state_enabled="false" android:drawable="@drawable/checkbox_off" />
</selector>
您的xml应将其声明为src:
<ImageView
android:id="@+id/img_checkmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/checkbox_selector_filter"/>
然后在代码中,确保根据事件/状态启用或禁用它:
imageCheckBox.isEnabled = true
对我来说这是最简单,最干净的
答案 9 :(得分:0)
如果您在单击时保存选择,然后重新加载图像滚动库(或滚动菜单),然后重新加载并滚动到具有替换图像的选择,那么您可能能够这样做。据我所知,内置的图库或菜单功能都无法在加载和显示图像后替换图像。
答案 10 :(得分:0)
您可以使用StateListDrawable
来实现此目的。此方法也适用于ImageButton
s。我更喜欢设置其他听众。
我还编写了一类额外的助手:http://alexanderwong.me/post/40799636705/android-change-background-image-drawable-on-press
public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable, Drawable disabledDrawable) {
boolean set = false;
StateListDrawable stateDrawable = new StateListDrawable();
if (disabledDrawable != null) {
set = true;
stateDrawable.addState(new int[] { -android.R.attr.state_enabled }, disabledDrawable);
}
if (pressedDrawable != null) {
set = true;
stateDrawable.addState(new int[] { android.R.attr.state_pressed }, pressedDrawable);
}
if (drawable != null) {
set = true;
stateDrawable.addState(new int[0], drawable);
}
return set ? stateDrawable : null;
}
答案 11 :(得分:0)
如果您有ImageView
或ImageButton
,并且想要在按下时更改其图像,则可以刷新任何按下的活动:
fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int isFavor = c.getInt(c.getColumnIndex("isFavor"));
if(isFavor == 1){
db.execSQL("update content set isFavor=0 where ID="+idxx+";");
fav.setImageResource(R.drawable.favnot_ic);
Toast.makeText(context.getApplicationContext(),"item cleaned",Toast.LENGTH_LONG).show();
activity.finish();
activity.overridePendingTransition(0, 0);
context.startActivity(activity.getIntent());
}
else{
db.execSQL("update content set isFavor=1 where ID=" + idxx + ";");
fav.setImageResource(R.drawable.fav_ic);
Toast.makeText(context.getApplicationContext(),"Item added...",Toast.LENGTH_LONG).show();
activity.finish();
activity.overridePendingTransition(0, 0);
context.startActivity(activity.getIntent());
}
}
});
答案 12 :(得分:0)
如果你已经在你的应用程序中有一个按钮,它将在你点击的两张照片之间进行交换,那么就有一个简单的代码:D
//在android中添加图片我们只是&#34;将它们从其位置复制并过去(res - &gt; drawable)&#34;
//然后拖放&#34; ImageView&#34;并选择您想要显示的图像
//你可以调整比例投射&#34; scaleType,layout_Width&amp; layout_Height&#34;
public boolean swap = true;
public void change(View view)
{
ImageView i = (ImageView) findViewById(R.id.img);
if (swap)
{
i.setImageResource(R.drawable.images);
swap=false;
}
else
{
swap=true;
i.setImageResource(R.drawable.couple);
}
}
答案 13 :(得分:0)
尽管回答为时已晚。但是有人可能会从此答案中获得帮助。
它就像魅力一样工作-
circularImageView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
circularImageView.setImageResource(R.drawable.profile_edit_photo);
break;
case MotionEvent.ACTION_UP:
circularImageView.setImageResource(R.drawable.photo_male_8);
break;
case MotionEvent.ACTION_MOVE:
Log.i("ImageViewEvent", "Action_Move_Called");
break;
}
return true; // Note: This return value must need to be true if you want to call MotionEvent.ACTION_UP: action.
}
});
答案 14 :(得分:-1)
Demo_button.setImageResource(R.drawable.secondimage)
//Demo_button is your event holder (the button or imageview or bitmap file which contains the image) and secondimage is your image (drawable) file, without any complications.
这就是诀窍。