我見過Android手機。當我點擊照片庫時,打開一個圖像(淡入),點擊後消失(淡出)。如何使用淡入淡出效果與使用過渡圖像,Android
同樣,我已經在我的應用程序中完成了。我在Drawable中粘貼了一張圖片。並應用淡入和淡出條件。但我沒有看到任何圖像比天藍色的背景。這是觀點。
我該如何在Android中以編程方式執行此操作?
以什麼方式可以解決這個問題?我在這裏做了什麼錯誤?
我的代碼是:
btn=(Button)findViewById(R.id.Click);
viewToAnimate=(View)findViewById(R.id.view1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(viewToAnimate.getVisibility()==View.VISIBLE)
{
boolean toRight = false;
Context c = null;
//Animation out=AnimationUtils.makeOutAnimation(this,true);
Animation out=AnimationUtils.makeOutAnimation(c, toRight);
viewToAnimate.startAnimation(out);
viewToAnimate.setVisibility(View.INVISIBLE);
} else {
int id = 0;
Context c = null;
// Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation in=AnimationUtils.loadAnimation(c, id);
viewToAnimate.startAnimation(in);
viewToAnimate.setVisibility(View.VISIBLE);
}
}
});
} }
然後main.xml中:
<Button
android:id="@+id/Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fade" />
<View
android:id="@+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AAA"
android:src="@drawable/fade"/>
錯誤在這行: - Animation out = AnimationUtils.loadAnimation(this,R.anim.fadein);錯誤是,類型AnimationUtils中的方法loadAnimation(Context,int)不適用於參數(new View.OnClickListener(){},int) –
您是否解決了上述錯誤,錯誤是由於您正在使用onClick事件的上下文(即loadAnimation函數中的** this **關鍵字),而不是它,您可以使用MyActivityName.this或getApplicationContext()。 –
是的,我解決了! –