最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)實(shí)現(xiàn)圖片切換APP

 更新時(shí)間:2020年12月22日 11:32:04   作者:無落  
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)圖片切換APP,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android開發(fā)實(shí)現(xiàn)圖片切換APP的具體代碼,供大家參考,具體內(nèi)容如下

本次介紹的是關(guān)于圖片切換的APP,這里實(shí)現(xiàn)了兩種切換效果;
不同的效果針對(duì)不同的情況,兩種效果的代碼都會(huì)介紹:

代碼-布局:

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數(shù)組
 private int imageIndex=0;//圖片顯示序列號(hào)

 @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(); //設(shè)置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() {//實(shí)現(xiàn)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;
 }
 /**
 * 獲得當(dāng)前界面數(shù)
 */
 @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);
 }
 /**
 * 判斷是否由對(duì)象生成界面
 */
 @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對(duì)象
 private ViewPagerAdapter vpAdapter;//定義ViewPager適配器
 private ArrayList<View> views;//定義一個(gè)ArrayList來存放View
 private static final int[] pics = {R.drawable.one,R.drawable.two,R.drawable.san,R.drawable.si};//引導(dǎo)圖片資源
 private ImageView[] points;//底部小點(diǎn)的圖片
 private int currentIndex;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 ActionBar actionBar=getSupportActionBar();//
 actionBar.hide();//隱藏標(biāo)題欄
 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);
 //初始化引導(dǎo)圖片列表
 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); //設(shè)置數(shù)據(jù)
 viewPager.setOnPageChangeListener(this);//設(shè)置監(jiān)聽
 initPoint();//初始化底部小點(diǎn)
 }

 private void initPoint() {
 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
 points = new ImageView[pics.length];
 //循環(huán)取得小點(diǎn)圖片
 for (int i = 0; i < pics.length; i++) {
  points[i] = (ImageView) linearLayout.getChildAt(i);//得到一個(gè)LinearLayout下面的每一個(gè)子元素
  points[i].setEnabled(true);//默認(rèn)都設(shè)為灰色
  points[i].setOnClickListener(this);//給每個(gè)小點(diǎn)設(shè)置監(jiān)聽
  points[i].setTag(i);//設(shè)置位置tag,方便取出與當(dāng)前位置對(duì)應(yīng)
 }
 currentIndex = 0;//設(shè)置當(dāng)面默認(rèn)的位置
 points[currentIndex].setEnabled(false);//設(shè)置為白色,即選中狀態(tài)
 }

 private void initView() {
 views = new ArrayList<View>();//實(shí)例化ArrayList對(duì)象
 viewPager = (ViewPager) findViewById(R.id.viewpager);//實(shí)例化ViewPager
 vpAdapter = new ViewPagerAdapter(views);//實(shí)例化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;
 }
}

最后的效果圖:

另外一種效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android如何獲取圖片或視頻略縮圖

    Android如何獲取圖片或視頻略縮圖

    這篇文章主要為大家詳細(xì)介紹了Android如何獲取圖片或視頻略縮圖的方法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android實(shí)現(xiàn)截屏與截長(zhǎng)圖功能

    Android實(shí)現(xiàn)截屏與截長(zhǎng)圖功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)截屏,以及Android實(shí)現(xiàn)截長(zhǎng)圖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android?Activity通用懸浮可拖拽View封裝的思路詳解

    Android?Activity通用懸浮可拖拽View封裝的思路詳解

    這篇文章主要介紹了Android?Activity通用懸浮可拖拽View封裝,實(shí)現(xiàn)思路是通過封裝通用的基礎(chǔ)懸浮View,繼承通用View,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • android實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄的示例

    android實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄的示例

    這篇文章主要介紹了android實(shí)現(xiàn)上滑屏幕隱藏底部菜單欄的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • Android中Textview和圖片同行顯示(文字超出用省略號(hào),圖片自動(dòng)靠右邊)

    Android中Textview和圖片同行顯示(文字超出用省略號(hào),圖片自動(dòng)靠右邊)

    Android中Textview和圖片同行顯示,文字超出用省略號(hào)顯示,圖片自動(dòng)靠右邊??吹竭@個(gè)問題本來認(rèn)為是一個(gè)很正常的需求,看起來很簡(jiǎn)單,但是做起來卻遇到了很蛋疼的問題,怎么搞的都不行,堵了很長(zhǎng)時(shí)間,下面說一下解決的方案,希望遇到這樣問題的朋友可以使用。
    2016-12-12
  • Flutter仿釘釘考勤日歷的示例代碼

    Flutter仿釘釘考勤日歷的示例代碼

    這篇文章主要介紹了Flutter仿釘釘考勤日歷的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 基于Android Flutter編寫貪吃蛇游戲

    基于Android Flutter編寫貪吃蛇游戲

    貪吃蛇是一款足夠經(jīng)典的游戲。本文將利用Android中的Flutter編寫這一經(jīng)典的小游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-03-03
  • kotlin實(shí)現(xiàn)五子棋單機(jī)游戲

    kotlin實(shí)現(xiàn)五子棋單機(jī)游戲

    這篇文章主要為大家詳細(xì)介紹了kotlin實(shí)現(xiàn)五子棋單機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Flutter多選按鈕組件Checkbox使用方法詳解

    Flutter多選按鈕組件Checkbox使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Flutter多選按鈕組件Checkbox使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android控件Spinner的使用方法(1)

    Android控件Spinner的使用方法(1)

    這篇文章主要為大家詳細(xì)介紹了Android控件Spinner的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評(píng)論

巴塘县| 湘乡市| 黎城县| 灵武市| 大连市| 兖州市| 蛟河市| 肥西县| 定南县| 伊金霍洛旗| 明光市| 邹平县| 东阿县| 禹城市| 伊川县| 东安县| 宁远县| 明光市| 东港市| 东光县| 玛沁县| 罗平县| 汉川市| 桦川县| 河东区| 大理市| 余江县| 昌图县| 泾阳县| 上栗县| 柳林县| 陆川县| 正镶白旗| 临西县| 双柏县| 连城县| 平凉市| 株洲市| 中阳县| 巴东县| 海晏县|