android图片转换器示例

 更新时间:2014年05月05日 09:13:49   作者:  
这篇文章主要介绍了android图片转换器示例,需要的朋友可以参考下

MainActivity.java

复制代码 代码如下:

package com.zhang.showPhoto;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

 private int[] imagId=new int[]{
   R.drawable.img01,R.drawable.img02,R.drawable.img03,R.drawable.img04,
   R.drawable.img05,R.drawable.img06,R.drawable.img07,R.drawable.img08,
   R.drawable.img09,R.drawable.img10
  };
 private int index=0;
 private ImageSwitcher imageSwitcher;
 private Button up,down;
 

 @Override
 protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  up=(Button) findViewById(R.id.bt1);
  down=(Button) findViewById(R.id.bt2);

 
  imageSwitcher=(ImageSwitcher) findViewById(R.id.imagSw1);
  imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
  imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
  imageSwitcher.setFactory(new ViewFactory() {

  
   public View makeView() {
    ImageView imageView = new ImageView(MainActivity.this);
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
      LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT
      ));
    return imageView;
   }
  });

  imageSwitcher.setImageResource(imagId[index]);

  up.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    if(index>0){
     index--;
    }else{
     index=imagId.length-1;
    }
    imageSwitcher.setImageResource(imagId[index]);
   }
  });

  down.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    if(index<imagId.length-1){
     index++;
    }else{
     index=0;
    }
    imageSwitcher.setImageResource(imagId[index]);
   }
  });
 }
}

main.xml

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg1"
    android:id="@+id/llayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张"
        android:id="@+id/bt1"
        />
    <ImageSwitcher
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imagSw1"
        android:layout_gravity="center"
        />
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张"
        android:id="@+id/bt2"
        />

</LinearLayout>

相关文章

  • Android实现倒计时结束后跳转页面功能

    Android实现倒计时结束后跳转页面功能

    最近在工作中遇到一个需求,需要在倒计时一段时间后进行跳转页面,通过查找相关资料发现其中涉及的知识还不少,所以分享出来,下面这篇文章主要给大家介绍了关于Android实现倒计时结束后跳转页面功能的相关资料,需要的朋友可以参考下。
    2017-11-11
  • Android 实现为点击事件添加震动效果

    Android 实现为点击事件添加震动效果

    这篇文章主要介绍了Android 实现为点击事件添加震动效果,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Android嵌套滚动与协调滚动的实现方式汇总

    Android嵌套滚动与协调滚动的实现方式汇总

    如何实现这种协调滚动的布局呢,我们使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior实现,另一种方案是MotionLayout,我们看看都是怎么实现的吧
    2022-06-06
  • Android应用开发中触摸屏手势识别的实现方法解析

    Android应用开发中触摸屏手势识别的实现方法解析

    这篇文章主要介绍了Android应用开发中触摸屏手势识别的实现方法解析,深入的部分则是对左右手势的识别给出了相关编写思路,需要的朋友可以参考下
    2016-02-02
  • Android开发之机顶盒上gridview和ScrollView的使用详解

    Android开发之机顶盒上gridview和ScrollView的使用详解

    这篇文章主要介绍了Android开发之机顶盒上gridview和ScrollView的使用详解的相关资料,需要的朋友可以参考下
    2016-02-02
  • Android App中各种数据保存方式的使用实例总结

    Android App中各种数据保存方式的使用实例总结

    这篇文章主要介绍了Android App中各种数据保存方式的使用实例,列举了SharedPreferences接口、机身空间存储、SD卡存储和SQLite数据库四种方式的代码例子,需要的朋友可以参考下
    2016-04-04
  • android实现短按电源键关机的实现代码

    android实现短按电源键关机的实现代码

    这篇文章主要介绍了android实现短按电源键关机的实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Android ViewPager实现无限循环效果

    Android ViewPager实现无限循环效果

    这篇文章主要为大家详细介绍了Android ViewPager实现无限循环效果的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Android仿微信朋友圈全文收起功能示例(附源码)

    Android仿微信朋友圈全文收起功能示例(附源码)

    本篇文章主要介绍了Android仿微信朋友圈全文收起功能示例(附源码),非常具有实用价值,需要的朋友可以参考下。
    2017-02-02
  • Android开发apk反编译和二次打包教程

    Android开发apk反编译和二次打包教程

    反编译不是让各位开发者去对一个应用破解搞重装什么的,主要目的是为了促进开发者学习,借鉴好的代码,提升自我开发水平。下面我们就来研究下如何进行APK反编译以及二次打包
    2016-04-04

最新评论