Android开发实现图片切换APP

论坛 期权论坛 脚本     
niminba   2021-5-23 02:58   1376   0

本文实例为大家分享了Android开发实现图片切换APP的具体代码,供大家参考,具体内容如下

本次介绍的是关于图片切换的APP,这里实现了两种切换效果;
不同的效果针对不同的情况,两种效果的代码都会介绍:

代码-布局:

main.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".MainActivity">

 <ImageSwitcher
 android:id="@+id/is_1"
 android:layout_width="match_parent"
 android:layout_height="243dp"
 android:layout_marginTop="68dp"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.0"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent" />

 <LinearLayout
 android:id="@+id/linearLayout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="68dp"
 android:orientation="horizontal"
 android:paddingTop="15dp"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/is_1">

 <Button
  android:id="@+id/btn_previous"
  style="?android:attr/borderlessButtonStyle"
  android:layout_width="0dip"
  android:layout_height="wrap_content"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="15dp"
  android:layout_weight="1"
  android:background="@drawable/shape_button_main"
  android:text="下一张"
  android:textColor="#ffffff"
  android:textSize="18dp" />

 <Button
  android:id="@+id/btn_next"
  style="?android:attr/borderlessButtonStyle"
  android:layout_width="0dip"
  android:layout_height="wrap_content"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="15dp"
  android:layout_weight="1"
  android:background="@drawable/shape_button_main"
  android:text="上一张"
  android:textColor="#ffffff"
  android:textSize="18dp" />
 </LinearLayout>

 <Button
 android:id="@+id/btn_3"
 android:layout_width="176dp"
 android:layout_height="80dp"
 android:layout_marginTop="8dp"
 android:layout_marginBottom="16dp"
 android:background="@drawable/shape_button_main"
 android:text="另外一种效果"
 android:textSize="20dp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

</android.support.constraint.ConstraintLayout>

mainactivity的代码:

package com.example.wuluo.yanqi;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,ViewSwitcher.ViewFactory{

 private ImageSwitcher is_1;
 private Button btn_next;
 private Button btn_previous;
 private Button btn_3;
 private int image[]={R.drawable.tian1,R.drawable.tian2,R.drawable.tian3,R.drawable.tian4};//图片的id数组
 private int imageIndex=0;//图片显示序列号

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 is_1=(ImageSwitcher) findViewById(R.id.is_1);
 btn_next=(Button) findViewById(R.id.btn_next);
 btn_previous=(Button) findViewById(R.id.btn_previous);
 btn_3=(Button)findViewById(R.id.btn_3);

 btn_previous.setOnClickListener(this);
 btn_next.setOnClickListener(this);
 btn_3.setOnClickListener(this);
 init(); //设置Factory
 }
 @Override
 public void onClick(View view) {
 if (view.getId()==R.id.btn_next){
  imageIndex++;
  if(imageIndex>3){
  imageIndex=0;
  }
  is_1.setInAnimation(this,R.anim.left_in);
  is_1.setOutAnimation(this,R.anim.right_out);
 }else if(view.getId()==R.id.btn_previous){
  imageIndex--;
  if(imageIndex<0){
  imageIndex=image.length-1;
  }
  is_1.setInAnimation(this,R.anim.right_in);
  is_1.setOutAnimation(this,R.anim.left_out);
 }else if(view.getId()==R.id.btn_3){
  Intent intent=new Intent();
  intent.setClass(this,other2.class);
  startActivity(intent);

 }
 is_1.setImageResource(image[imageIndex]);
 }

 @Override
 public View makeView() {//实现viewFactory接口.生成imageview
 ImageView imageView=new ImageView(this);
 return imageView;
 }
 private void init(){//初始化imageSwitch
 is_1.setFactory(this);
 is_1.setImageResource(image[imageIndex]);
 }

}

ViewPagerAdapter的代码:

package com.example.wuluo.yanqi;


/**
 * Created by wuluo on 2018/12/21
 */
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
importYBY\\H]Y]YY\\Y]NY]Y` acyfjBCB\YCBXXYY
[K[LJHBCBB\YCBXXYXY
[JHJNCBB\YCBXXY]P[
[JHBCBB\YCBXXXY]Y][][H
[Y]Y]˙YNY]][N][NBCBB]]HY][][^Y
][	][	X[
H]\CBY]Y[][J][NCB]]H[]^Y
]	]	X[HH[[^OH]H]\CB]K[XY
[N[[^K[XY
YJN[[^H]CBOOB]B 9d#b99f&B[OH^X[Y\[Y[HZZ[Z[[^][KNKLLNN

NXM
MLMBc%. 9b99f&B[OH^X[Y\[Y[HZZ[Z[[^][KNKXYY
YMMB."l,y+j:`yk{n#9&)9ki.h9"y`9n+b{.g&i&i&+/c.
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP