电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android開發實現圖片切換APP

瀏覽:45日期:2022-09-21 11:40:50

本文實例為大家分享了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: 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: 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: 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: 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: 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: 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: 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='http://www.hdgsjgj.cn/bcjs/@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='http://www.hdgsjgj.cn/bcjs/@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='http://www.hdgsjgj.cn/bcjs/@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='http://www.hdgsjgj.cn/bcjs/@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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 标策网-专注公司商业知识服务、助力企业发展 | 整合营销推广|营销网络推广公司|石家庄网站优化推广公司|智营销 好物生环保网、环保论坛 - 环保人的学习交流平台 | 铁盒_铁罐_马口铁盒_马口铁罐_铁盒生产厂家-广州博新制罐 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 刺绳_刀片刺网_刺丝滚笼_不锈钢刺绳生产厂家_安平县浩荣金属丝网制品有限公司-安平县浩荣金属丝网制品有限公司 | 2025第九届世界无人机大会| 中央空调维修、中央空调保养、螺杆压缩机维修-苏州东菱空调 | 移动机器人产业联盟官网 | 私人别墅家庭影院系统_家庭影院音响_家庭影院装修设计公司-邦牛影音 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 撕碎机,撕破机,双轴破碎机-大件垃圾破碎机厂家 | 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 喷砂机厂家_自动除锈抛丸机价格-成都泰盛吉自动化喷砂设备 | 福州甲醛检测-福建室内空气检测_环境检测_水质检测-福建中凯检测技术有限公司 | 铁艺,仿竹,竹节,护栏,围栏,篱笆,栅栏,栏杆,护栏网,网围栏,厂家 - 河北稳重金属丝网制品有限公司 山东太阳能路灯厂家-庭院灯生产厂家-济南晟启灯饰有限公司 | 常州企业采购平台_常州MRO采购公司_常州米孚机电设备有限公司 | 广州展览制作|展台制作工厂|展览设计制作|展览展示制作|搭建制作公司 | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 玉米深加工设备|玉米加工机械|玉米加工设备|玉米深加工机械-河南成立粮油机械有限公司 | 设计圈 - 让设计更有价值!| 软文推广发布平台_新闻稿件自助发布_媒体邀约-澜媒宝 | Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 耐压仪-高压耐压仪|徐吉电气| 精准猎取科技资讯,高效阅读科技新闻_科技猎| 水平垂直燃烧试验仪-灼热丝试验仪-漏电起痕试验仪-针焰试验仪-塑料材料燃烧检测设备-IP防水试验机 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 深圳宣传片制作_产品视频制作_深圳3D动画制作公司_深圳短视频拍摄-深圳市西典映画传媒有限公司 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 净化车间装修_合肥厂房无尘室设计_合肥工厂洁净工程装修公司-安徽盛世和居装饰 | 早报网| 深圳市万色印象美业有限公司| 南京种植牙医院【官方挂号】_南京治疗种植牙医院那个好_南京看种植牙哪里好_南京茀莱堡口腔医院 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 选矿设备-新型重选设备-金属矿尾矿重选-青州冠诚重工机械有限公司 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 膜结构停车棚-自行车棚-膜结构汽车棚加工安装厂家幸福膜结构 | 广东西屋电气有限公司-广东西屋电气有限公司| 南京办公用品网-办公文具用品批发-打印机耗材采购 |