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自定义ImageView实现在图片上添加图层效果

    Android自定义ImageView实现在图片上添加图层效果

    这篇文章给大家主要介绍了利用Android自定义ImageView如何实现在图片上添加图层的效果,实现的效果类似在图片增加秒杀、抢光等标签图片,对大家开发的时候具有一定的参考借鉴价值,有需要的朋友们下面来一起学习学习吧。
    2016-11-11
  • Android详细讲解谷歌推出的官方二维码扫描库

    Android详细讲解谷歌推出的官方二维码扫描库

    Google推出的官方二维码扫描库你知道吗?还不知道就落伍咯!本篇文字带你了解google二维码扫描库的详细情况与使用,还不知道的朋友快来看看吧
    2022-03-03
  • Android事件与手势操作详解

    Android事件与手势操作详解

    现在程序都是通过事件实现人机交互的。事件:用于对图形界面的操作,有物理按键事件和触摸屏事件两大类。在Android组件中提供了事件处理的相关方法,如在View类中提供onTouchEvent()方法重写触摸屏事件,setOnTouchListener()方法为组件设置监听器来处理触摸屏事件
    2022-12-12
  • Android BroadcastReceiver接收收到短信的广播

    Android BroadcastReceiver接收收到短信的广播

    这篇文章主要为大家详细介绍了Android BroadcastReceiver接收收到短信的广播,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android使用SoundPool播放音效

    Android使用SoundPool播放音效

    这篇文章主要为大家详细介绍了Android使用SoundPool播放音效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android工具类ImgUtil选择相机和系统相册

    Android工具类ImgUtil选择相机和系统相册

    这篇文章主要为大家详细介绍了Android工具类ImgUtil选择相机和系统相册,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-10-10
  • Android Studio 报Integer types not allowed错误

    Android Studio 报Integer types not allowed错误

    本文给大家分享的是在使用Android Studio的过程中遇到的报Integer types not allowed错误的分析及解决方法,非常实用,有需要的小伙伴可以参考下
    2017-10-10
  • Android中使用SDcard读取文件

    Android中使用SDcard读取文件

    这篇文章主要介绍了Android中使用SDcard读取文件的相关资料,需要的朋友可以参考下
    2016-02-02
  • Android仿微信底部菜单栏功能显示未读消息数量

    Android仿微信底部菜单栏功能显示未读消息数量

    这篇文章主要介绍了Android仿微信底部菜单栏功能,并显示未读消息数量,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android自定义短信验证码组件

    Android自定义短信验证码组件

    这篇文章主要为大家详细介绍了Android自定义短信验证码组件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01

最新评论