欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发实现图片切换APP

程序员文章站 2022-09-02 11:29:26
本文实例为大家分享了android开发实现图片切换app的具体代码,供大家参考,具体内容如下本次介绍的是关于图片切换的app,这里实现了两种切换效果;不同的效果针对不同的情况,两种效果的代码都会介绍:...

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

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

代码-布局:

Android开发实现图片切换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;
import java.util.arraylist;

public class viewpageradapter extends pageradapter {
 //界面列表
 private arraylist<view> views;
 public viewpageradapter(arraylist<view> views) {
 this.views = views;
 }
 /**
 * 获得当前界面数
 */
 @override
 public int getcount() {
 if (views != null) {
  return views.size();
 }
 return 0;
 }
 /**
 * 初始化position位置的界面
 */
 @override
 public object instantiateitem(view view, int position) {

 ((viewpager) view).addview(views.get(position), 0);

 return views.get(position);
 }
 /**
 * 判断是否由对象生成界面
 */
 @override
 public boolean isviewfromobject(view view, object arg1) {
 return (view == arg1);
 }
 /**
 * 销毁position位置的界面
 */
 @override
 public void destroyitem(view view, int position, object arg2) {
 ((viewpager) view).removeview(views.get(position));
 }
}

other2.xml布局的代码:

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

 <android.support.v4.view.viewpager
 android:id="@+id/viewpager"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 app:layout_constraintbottom_tobottomof="parent"
 app:layout_constraintend_toendof="parent"
 app:layout_constrainthorizontal_bias="0.0"
 app:layout_constraintstart_tostartof="parent"
 app:layout_constrainttop_totopof="parent"
 app:layout_constraintvertical_bias="0.0" />

 <linearlayout
 android:id="@+id/ll"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignparentbottom="true"
 android:layout_centerhorizontal="true"
 android:layout_marginstart="8dp"
 android:layout_marginend="8dp"
 android:layout_marginbottom="8dp"
 android:orientation="horizontal"
 app:layout_constraintbottom_tobottomof="@+id/viewpager"
 app:layout_constraintend_toendof="parent"
 app:layout_constraintstart_tostartof="parent">

 <imageview
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_gravity="center_vertical"
  android:clickable="true"
  android:padding="15.0dip"
  android:src="@drawable/point" />

 <imageview
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_gravity="center_vertical"
  android:clickable="true"
  android:padding="15.0dip"
  android:src="@drawable/point" />

 <imageview
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_gravity="center_vertical"
  android:clickable="true"
  android:padding="15.0dip"
  android:src="@drawable/point" />

 <imageview
  android:layout_width="wrap_content"
  android:layout_height="40dp"
  android:layout_gravity="center_vertical"
  android:clickable="true"
  android:padding="15.0dip"
  android:src="@drawable/point" />
 </linearlayout>
</android.support.constraint.constraintlayout>

other2activity的代码:

package com.example.wuluo.yanqi;

import android.support.v4.view.viewpager;
import android.support.v7.app.actionbar;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.imageview;
import android.widget.linearlayout;

import java.util.arraylist;

public class other2 extends appcompatactivity implements view.onclicklistener,viewpager.onpagechangelistener{
 private viewpager viewpager;//定义viewpager对象
 private viewpageradapter vpadapter;//定义viewpager适配器
 private arraylist<view> views;//定义一个arraylist来存放view
 private static final int[] pics = {r.drawable.one,r.drawable.two,r.drawable.san,r.drawable.si};//引导图片资源
 private imageview[] points;//底部小点的图片
 private int currentindex;
 @override
 protected void oncreate(bundle savedinstancestate) {
 actionbar actionbar=getsupportactionbar();//
 actionbar.hide();//隐藏标题栏
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_other2);
 initview();
 initdata();
 }

 private void initdata() {
 linearlayout.layoutparams mparams = new linearlayout.layoutparams(linearlayout.layoutparams.fill_parent,
  linearlayout.layoutparams.fill_parent);
 //初始化引导图片列表
 for(int i=0; i<pics.length; i++) {
  imageview iv = new imageview(this);
  iv.setlayoutparams(mparams);
  iv.setimageresource(pics[i]);
  views.add(iv);
 }
 viewpager.setadapter(vpadapter); //设置数据
 viewpager.setonpagechangelistener(this);//设置监听
 initpoint();//初始化底部小点
 }

 private void initpoint() {
 linearlayout linearlayout = (linearlayout) findviewbyid(r.id.ll);
 points = new imageview[pics.length];
 //循环取得小点图片
 for (int i = 0; i < pics.length; i++) {
  points[i] = (imageview) linearlayout.getchildat(i);//得到一个linearlayout下面的每一个子元素
  points[i].setenabled(true);//默认都设为灰色
  points[i].setonclicklistener(this);//给每个小点设置监听
  points[i].settag(i);//设置位置tag,方便取出与当前位置对应
 }
 currentindex = 0;//设置当面默认的位置
 points[currentindex].setenabled(false);//设置为白色,即选中状态
 }

 private void initview() {
 views = new arraylist<view>();//实例化arraylist对象
 viewpager = (viewpager) findviewbyid(r.id.viewpager);//实例化viewpager
 vpadapter = new viewpageradapter(views);//实例化viewpager适配器
 }
 @override
 public void onpagescrolled(int i, float v, int i1) {

 }

 @override
 public void onpageselected(int i) {
 setcurdot(i);
 }

 @override
 public void onpagescrollstatechanged(int i) {

 }

 @override
 public void onclick(view view) {
 int position = (integer)view.gettag();
 setcurview(position);
 setcurdot(position);

 }

 private void setcurview(int position){
 if (position < 0 || position >= pics.length) {
  return;
 }
 viewpager.setcurrentitem(position);
 }
 private void setcurdot(int positon){
 if (positon < 0 || positon > pics.length - 1 || currentindex == positon) {
  return;
 }
 points[positon].setenabled(false);
 points[currentindex].setenabled(true);
 currentindex = positon;
 }
}

最后的效果图:

Android开发实现图片切换APP

另外一种效果图:

Android开发实现图片切换APP

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。