私はギャラリーを作っています。これは画像を開いたとき、フル画像でimage1と言った後、右方向にスワイプします。image1の次の画像、つまりimage2が来て、image2の画像は表示されません。 image1の逆方向にクリックします。 画像はスワイプされませんか?
FullimageActivity.java
package com.example.android.helloapp;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
public class Full_ImageActivity extends AppCompatActivity {
float startXValue = 1;
ImageView fullImageView;
int position;
private Integer[] mImageIds = { R.drawable.gallery_one,R.drawable.gallery_two,R.drawable.gallery_three,R.drawable.gallery_four,R.drawable.gallery_five,
R.drawable.gallery_six,R.drawable.gallery_seven,R.drawable.gallery_eight,R.drawable.gallery_nine,R.drawable.gallery_ten,
R.drawable.gallery_eleven,R.drawable.gallery_twelve,R.drawable.gallery_thirteen,R.drawable.gallery_fourteen,R.drawable.gallery_fifteen,
R.drawable.gallery_sixteen,R.drawable.gallery_seventeen,R.drawable.gallery_eighteen,R.drawable.gallery_nineteen,
R.drawable.gallery_twenty,R.drawable.gallery_twenty_one,R.drawable.gallery_twenty_two,R.drawable.gallery_twenty_three,R.drawable.gallery_twenty_four,
R.drawable.gallery_twenty_five};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_full__image);
} else {
setContentView(R.layout.activity_full__image);
}
Intent i=getIntent();
position=i.getExtras().getInt("id");
ImageAdapter imageAdapter=new ImageAdapter(this);
fullImageView=(ImageView)findViewById(R.id.fullImageView);
fullImageView.setImageResource(imageAdapter.images[position]);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int orientation;
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
// or = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}else {
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
// Add code if needed
setRequestedOrientation(orientation);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float endXValue=0;
float x1=event.getAxisValue(MotionEvent.AXIS_X);
int action= MotionEventCompat.getActionMasked(event);
switch (action){
case (MotionEvent.ACTION_DOWN):
startXValue=event.getAxisValue(MotionEvent.AXIS_X);
return true;
case (MotionEvent.ACTION_UP):
endXValue = event.getAxisValue(MotionEvent.AXIS_X);
if (endXValue > startXValue) {
if (endXValue - startXValue > 100) {
System.out.println("Left-Right");
if(position-1!=-1)
fullImageView.setImageResource(mImageIds[position-1]);
}
}else {
if (startXValue -endXValue> 100) {
System.out.println("Right-Left");
if(position+1!=mImageIds.length)
fullImageView.setImageResource(mImageIds[position+1]);
}
}
return true;
default:
return super.onTouchEvent(event);
}
}
}
fullimage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_full__image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.helloapp.Full_ImageActivity">
<include layout="@layout/tool_bar"></include>
<ImageView
android:id="@+id/fullImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:layout_gravity="center"
/>
</LinearLayout>
ImageAdapter.java
package com.example.android.helloapp;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context context;
public Integer[] images={
R.drawable.gallery_one,R.drawable.gallery_two,R.drawable.gallery_three,R.drawable.gallery_four,R.drawable.gallery_five,
R.drawable.gallery_six,R.drawable.gallery_seven,R.drawable.gallery_eight,R.drawable.gallery_nine,R.drawable.gallery_ten,
R.drawable.gallery_eleven,R.drawable.gallery_twelve,R.drawable.gallery_thirteen,R.drawable.gallery_fourteen,R.drawable.gallery_fifteen,
R.drawable.gallery_sixteen,R.drawable.gallery_seventeen,R.drawable.gallery_eighteen,R.drawable.gallery_nineteen,
R.drawable.gallery_twenty,R.drawable.gallery_twenty_one,R.drawable.gallery_twenty_two,R.drawable.gallery_twenty_three,R.drawable.gallery_twenty_four,
R.drawable.gallery_twenty_five
};
public ImageAdapter(Context c){
context=c;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView=new ImageView(context);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new GridView.LayoutParams(240,240));
return imageView;
}
}
viewPagerを使用しています。 [ここをクリック](https://developer.android.com/reference/android/support/v4/view/ViewPager.html) –
ジェスチャー検出の代わりにViewPagerを使用しなかったのはなぜですか? –
アンドロイドで画像を扱う場合、特に多くの画像を一度に扱う場合は、メモリ管理について考える必要があります。上記の提案と同様に、私はあなたにViewPagerとFragmentStatePagerAdapterを使用することをお勧めします – faruk