0
嗨,我正在寫一個應用程序,我需要給一個ImageView兩個不同的背景;但是,我不知道該怎麼做。如何在Android中動態設置2個背景到單個ImageView?
在我的情況下,我需要設置一個動畫和背景圖像到一個ImageView;但它只顯示動畫而不是圖像背景。
我已經寫了圖像背景作爲ic_launcher
圖像,但它沒有顯示,只有動畫顯示。我需要同時使用相同的ImageView來顯示。誰能幫我?
我的代碼:
public class MainActivity extends Activity {
AnimationDrawable Tranninganimation3;
ImageView unlockimg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tranninganimation3 = new AnimationDrawable();
Tranninganimation3.setOneShot(false);
new playninzi3().onPreExecute();
unlockimg=(ImageView)findViewById(R.id.img);
unlockimg.setBackgroundResource(R.drawable.ic_launcher);
unlockimg.setBackgroundDrawable(Tranninganimation3);
unlockimg.post(new Starter3());
}
public class playninzi3 extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
try {
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated000copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated002copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated004copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated006copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated008copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated010copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated012copy), 150);
Tranninganimation3.addFrame(
getResources()
.getDrawable(R.drawable.glow_animated014copy), 150);
} catch (Exception e) {
}
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return null;
}
}
class Starter3 implements Runnable {
@Override
public void run() {
Tranninganimation3.start();
}
}
}
創建LayerDrawable有兩個層次:一個正常的,靜態BitmapDrawable和第二AnimationDrawable – pskink
所以閱讀它的文檔的http:/ /developer.android.com/reference/android/graphics/drawable/LayerDrawable.html – pskink
嗨,謝謝..我嘗試使用layerdrawable其工作..資源r = getResources(); Drawable [] layers = new Drawable [2]; layers [0] = r.getDrawable(R.drawable.ic_launcher); layers [1] = Tranninganimation3; LayerDrawable layerDrawable = new LayerDrawable(layers); unlockimg.setImageDrawable(layerDrawable); – user2401554