「次へ」と「前へ」という2つのボタンを含む画像切り替えを作成します。 「次へ」ボタンをクリックすると次の画像に切り替わり、「前の」ボタンは前の画像に切り替わります。私は配列を使用してすべての画像URLを保存しています。画像1を持っていて、「次へ」ボタンをクリックすると画像2に切り替わります。今のところ、私は2つのイメージの間でのみ切り替えることができます、私のコードです。ボタンをクリックすると異なる画像が表示されます
更新: 私は自分のコードを更新しましたが、今度は画像を切り替えることができますが、アプリはしばらくするとクラッシュします。何か案は?新しいAPIあたりとしてimageLoader.get(url)
からloadurl(url)
へ
XML
<ImageSwitcher
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:id="@+id/btnleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="23dp"
android:layout_marginStart="90dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:src="@android:drawable/ic_media_rew"
android:background="@android:color/holo_green_light"/>
<ImageButton
android:id="@+id/btnRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="91dp"
android:layout_alignTop="@+id/btnleft"
android:layout_alignParentEnd="true"
android:src="@android:drawable/ic_media_ff"
android:background="@android:color/holo_green_light"/>
Main.java
public class MainActivity extends AppCompatActivity {
private ImageSwitcher imageSwitcher;
private ImageButton buttonLeft, buttonRight;
public static int currentIndex=0;
private static String url[] = new String[]{
"https://pbs.twimg.com/profile_images/875443327835025408/ZvmtaSXW_400x400.jpg",
"http://www.ipbrief.net/wp-content/uploads/2012/05/262px-Android_dance.svg_.png",
"http://quizzzz.net/en/logo/images/1342886140.jpg",
"https://androiddevsimplified.files.wordpress.com/2016/01/android-logo-png-05073.png"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher)findViewById(R.id.images);
buttonLeft = (ImageButton)findViewById(R.id.btnleft);
buttonRight = (ImageButton)findViewById(R.id.btnRight);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// Returns the context for the entire application (the process all the Activities are running inside of)
ImageView imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
return imageView;
}
});
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
buttonRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(currentIndex < url.length)
LoadingImage(url[currentIndex++]);
}
});
buttonLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(currentIndex > 0)
LoadingImage(url[currentIndex--]);
}
});
}
private void LoadingImage(final String url){
ImageLoader imageLoader = Singleton.getInstance(getApplicationContext()).getImageLoader();
imageLoader.get(url, new ImageLoader.ImageListener(){
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.getMessage());
}
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
Bitmap image = response.getBitmap();
Drawable drawable = new BitmapDrawable(getResources(),image);
imageSwitcher.setImageDrawable(drawable);
}
});
}
}
なぜあなたは固定URL [0]とURL [1]だけを使用するのですか? – nhoxbypass