Android ImageView 实现图片触屏左右、上下以及按钮切换图片

布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--suppress ALL -->
 3 <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     android:orientation="vertical"
 7     android:gravity="center"
 8     xmlns:android="http://schemas.android.com/apk/res/android">
 9     <LinearLayout
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content">
12 
13 <!--        <ImageView android:id="@+id/image"
14             android:layout_width="355dip"
15             android:layout_height="650dip"
16             android:src="@drawable/p2"/>-->
17         <ImageSwitcher
18             android:layout_width="355dp"
19             android:layout_height="650dp"
20             android:id="@+id/image"
21             />
22     </LinearLayout>
23     <LinearLayout
24         android:layout_width="match_parent"
25         android:layout_height="fill_parent">
26         <Button android:id="@+id/up"
27             android:layout_width="150dip"
28             android:layout_height="wrap_content"
29             android:text="上一张"/>
30         <Button android:id="@+id/down"
31             android:layout_width="150dip"
32             android:layout_height="wrap_content"
33             android:text="下一张"/>
34     </LinearLayout>
35 </LinearLayout>

JAVA代码:

  1 package com.android.booktest;
  2 
  3 import androidx.appcompat.app.AppCompatActivity;
  4 
  5 import android.os.Bundle;
  6 import android.util.Log;
  7 import android.view.MotionEvent;
  8 import android.view.View;
  9 import android.view.animation.Animation;
 10 import android.view.animation.AnimationUtils;
 11 import android.widget.Button;
 12 import android.widget.ImageSwitcher;
 13 import android.widget.ImageView;
 14 import android.widget.ViewSwitcher;
 15 
 16 public class MainActivity extends AppCompatActivity {
 17     Button Button1,Button2;
 18     int[] images={
 19             R.drawable.p1, R.drawable.p2,R.drawable.p3
 20     };
 21 //    ImageView image;
 22     ImageSwitcher image;
 23     private float x;
 24     private float y;
 25     private float dx;
 26     private float dy;
 27     int index=0;
 28     @Override
 29     protected void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.activity_main);
 32         Button1 = (Button)findViewById(R.id.up);
 33         Button2 = (Button)findViewById(R.id.down);
 34         image = (ImageSwitcher) findViewById(R.id.image);
 35         // 必须设置
 36         image.setFactory(new ViewSwitcher.ViewFactory() {
 37             @Override
 38             public View makeView() {
 39                 ImageView imageView=new ImageView(MainActivity.this);
 40                 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
 41                 return imageView;
 42             }
 43         });
 44         image.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
 45         image.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
 46         image.setImageResource(images[0]);
 47         Button1.setOnClickListener(new mclik());
 48         Button2.setOnClickListener(new mclik());
 49         image.setOnTouchListener(new touchme());
 50     }
 51 
 52     class mclik implements View.OnClickListener{
 53 
 54         @Override
 55         public void onClick(View v) {
 56             if(v==Button1){
 57                 backpic();
 58             }
 59             if(v==Button2){
 60                 nextpic();
 61             }
 62         }
 63     }
 64 
 65     private void backpic(){
 66         if(index>0&&index<images.length){
 67             index--;
 68             image.setImageResource(images[index]);
 69         }else {
 70             index=images.length-1;
 71             image.setImageResource(images[index]);
 72         }
 73     }
 74 
 75     private void nextpic(){
 76         if(index>=0&&index<images.length-1){
 77             index++;
 78             image.setImageResource(images[index]);
 79         }else {
 80             index=0;
 81             image.setImageResource(images[index]);
 82         }
 83     }
 84 
 85     //左右滑动
 86 /*
 87     private class touchme implements View.OnTouchListener {
 88 
 89         @Override
 90         public boolean onTouch(View v, MotionEvent event) {
 91             if(event.getAction()==MotionEvent.ACTION_DOWN){
 92                 x = event.getX();
 93                 y = event.getY();
 94             } else if (event.getAction() == MotionEvent.ACTION_UP)  {
 95                 dx = event.getX() - x;
 96                 dy = event.getY() - y;
 97                 if (dx > 0 && Math.abs(dy)<50 && Math.abs(dy)>0) {
 98                     nextpic();
 99                 } else if (dx < 0 && Math.abs(dy)<50 && Math.abs(dy)>0) {
100                     backpic();
101                 }
102             }
103             return true;
104         }
105     }
106 */
107 
108     //上下滑动
109     private class touchme implements View.OnTouchListener {
110 
111         @Override
112         public boolean onTouch(View v, MotionEvent event) {
113             if(event.getAction()==MotionEvent.ACTION_DOWN){
114                 x = event.getX();
115                 y = event.getY();
116             } else if (event.getAction() == MotionEvent.ACTION_UP)  {
117                 dx = event.getX() - x;
118                 dy = event.getY() - y;
119                 if (dy > 0 && Math.abs(dx)<50 && Math.abs(dx)>0) {
120                     nextpic();
121                 } else if (dy < 0 && Math.abs(dx)<50 && Math.abs(dx)>0) {
122                     backpic();
123                 }
124             }
125             return true;
126         }
127     }
128 }

 

posted @ 2020-11-27 17:54  冯寳寳  阅读(684)  评论(0编辑  收藏  举报