本文实例为大家分享了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;
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;
 }
}

最后的效果图:

另外一种效果图:

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

Android开发实现图片切换APP的更多相关文章

  1. 详解如何通过H5(浏览器/WebView/其他)唤起本地app

    这篇文章主要介绍了详解如何通过H5(浏览器/WebView/其他)唤起本地app的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. html5唤起app的方法

    这篇文章主要介绍了html5唤起app的方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  7. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  8. xcode – 上传到App Store时进行身份验证

    只需为现有安装/文件夹创建备份,这很重要,因为在(新)安装期间,Transporter将删除以前的安装:现在运行以下命令来更新Transporter:希望这有助于某人.

  9. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  10. App store拒绝应用程序在iOs 10上支持IPV6网络

    我收到苹果公司的app拒绝邮件,下面是我们在连接到IPv6网络的Wi-Fi上运行iOS10.0.2的iPad和iPhone上查看了应用中的一个或多个错误.具体来说,应用程序在启动时仍保留在启动屏根据他们的要求,我已经在我的Mac上创建了NAT64网络,并为iPhone5S设备10.0.2os版本共享了互联网,App工作正常,但苹果称其不与IPv6合作任何人都可以确认我需要检查其他什么吗?

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部