我有一個帶有大量圖像按鈕的滾動視圖。我想在按下按鈕時更改圖像按鈕的圖像。事情是,我希望圖像保持到另一個圖像按鈕被按下。這就是爲什麼我不能使用選擇器。實現他的最佳做法是什麼?按下時更改圖像視圖的源圖像
問候
我有一個帶有大量圖像按鈕的滾動視圖。我想在按下按鈕時更改圖像按鈕的圖像。事情是,我希望圖像保持到另一個圖像按鈕被按下。這就是爲什麼我不能使用選擇器。實現他的最佳做法是什麼?按下時更改圖像視圖的源圖像
問候
你想這樣做。
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);
}
}
試試這個。 (更新的設置集)
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);
}
}
我希望你想要那樣 對嗎?
不是。我正在尋找一個帶有視圖的滾動視圖,其中,如果您選擇一個視圖,最後一個選定的視圖將被取消選中。您的示例適用於2個按鈕,並且選擇是手動完成的。 – Gratzi 2010-11-18 07:46:26
如果在點擊後保存選擇,然後重新加載圖像滾動畫廊(或滾動菜單),然後重新加載並使用替換圖像滾動到所選內容,則可能可以完成此操作。就我所知,內置的圖庫或菜單功能在加載和顯示後都不能替換圖像。
我正在考慮像創建一個自定義按鈕,並使用一種方法(如onSelected,但選擇另一個按鈕時用false調用)來設置當前圖像。 – Gratzi 2010-11-22 07:25:58
的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;
}
});
這比在'onClick()'中一個接一個地調用'image.setImageResource(R.drawable.image_when_pressed)'和'image.setImageResource(R.drawable.image_when_released)'要好。如果用戶持有圖片,資源將保持'image_when_pressed'直到他們釋放按鈕 – 2014-05-07 14:58:42
可以使用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;
}
別忘了創建字段「好玩」
試試這個改變image.When 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();
}
}
});
試試下面的代碼: -
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;
}
}
}
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;
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.
該做的伎倆。
更好的解決方案使用下面的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());
}
另請參閱我的答案,它更短,不需要Java代碼:https://stackoverflow.com/a/45239629/245966(和它也會在Android Studio中生成預覽) – 2017-07-21 13:58:23
什麼工作對我來說是:
我創建了一個新的可繪製的XML文件,例如,image_state。XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:src="@drawable/image_pressed"/>
</item>
<item>
<bitmap android:src="@drawable/image"/>
</item>
</selector>
而在我的佈局文件,我設置的ImageView的src爲:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_state"/>
如果你有一個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());
}
}
});
這似乎是實現這一目標的最短途徑,並且只需要XML,沒有Java代碼:
將文件保存爲繪製/ 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" />
如果u在烏爾應用已經一個按鈕,將做這兩個圖片之間的交換你有點擊時,然後有一個簡單的代碼:D
//在Android中添加圖片我們只是「從他們的位置複製他們d在過去(RES - >繪製) 「
//然後拖放 」ImageView的「,並選擇ü要顯示
// U可以調整刻度擲圖像」 scaleType,layout_Width & layout_Height 「
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);
}
}
嗨PrashantAdesara。我知道這將工作(我將不得不設置爲先前點擊查看未選中的圖像)。我以更自然的方式思考。就像創建自定義圖像視圖並在其中設置當前資源圖像一樣(例如在setSelected方法中)。問題是我需要一個視圖方法,當視圖被選中和未選中時被調用。希望這是明確的。 – Gratzi 2010-11-16 11:24:53