我有relative layout
,其中包含图片。我喜欢drawable
文件夹中的10-20张图片和存储这些图片的hashmap
。
在从左到右滑动图像下方时,它应该更改为下一张图像。
在每次滑动时,它应该从hashmap
更改为下一张图像。
我对java和android编程完全陌生,并以任何方式提供帮助。
这就是我所拥有的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bschiranth.homework2_chiranthbs.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/alice"
android:onClick="onClick"
android:clickable="true"
android:src="@drawable/alice"/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/zoombar"
android:max="100"
android:progress="50"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
和我的java:
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d("fling", "doing fling");
MovieData movieData = new MovieData();
HashMap hashMap = new HashMap();
for(int i=1;i<movieData.getSize();i++)
{
hashMap = movieData.getItem(i);
}
if(event1.getX()-event2.getX()> SWIPE_MIN_DISTANCE && Math.abs(velocityX)>SWIPE_THRESHOLD_VELOCITY)
{
Toast.makeText(Activity_Control.this,
"its flinging right to left", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.avatar);
}
if(event2.getX()-event1.getX()> SWIPE_MIN_DISTANCE && Math.abs(velocityX)>SWIPE_THRESHOLD_VELOCITY)
{
Toast.makeText(Activity_Control.this,
"its flinging left to right", Toast.LENGTH_SHORT).show();
imageView.setImageResource(R.drawable.avengers);
}
return true;
}
答案 0 :(得分:0)
最好使用view pager而不是imageview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/myfivepanelpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
ViewPagerAdapter.java
import android.app.Activity;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
PageIndicatorActivity.java
public class PageIndicatorActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = { R.drawable.antartica1, R.drawable.antartica2,
R.drawable.antartica3, R.drawable.antartica4,
R.drawable.antartica5, R.drawable.antartica6,
R.drawable.antartica7, R.drawable.antartica8 };
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
答案 1 :(得分:0)
这可以通过ViewFlipper
完成,如下所述。
这是您的Activity.xml
文件
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HowToUseActivity" >
<ImageView
android:id="@+id/imageHowToUse"
android:contentDescription="@string/app_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@mipmap/screen_1" />
</ViewFlipper>
在您的活动类中声明所需的变量
private int number = 0;
private float initialX;
private ImageView imageView;
在Activity
的{{1}}方法中检索onCreate(...)
ImageView
覆盖同一活动中的方法imageView = (ImageView) findViewById(R.id.imageHowToUse);
onTouchEvent(...)
然后编写方法@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = event.getX();
break;
case MotionEvent.ACTION_UP:
float finalX = event.getX();
if (initialX > finalX) {
if (number >= 3) {
break;
}
next();
} else {
if(number <= 0) {
break;
}
previous();
}
break;
}
return false;
}
private void next() {
number++;
imageView.setImageResource(getImage());
}
private void previous() {
number--;
imageView.setImageResource(getImage());
}
以获取您想要显示的下一个图像
getImage()
这是我项目中的工作代码。它会帮助你。
注意:R.mipmap.screen_ *是我资源目录中的图像资源。