android圖片轉(zhuǎn)換器示例
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>
相關(guān)文章
Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁面功能
最近在工作中遇到一個(gè)需求,需要在倒計(jì)時(shí)一段時(shí)間后進(jìn)行跳轉(zhuǎn)頁面,通過查找相關(guān)資料發(fā)現(xiàn)其中涉及的知識(shí)還不少,所以分享出來,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)倒計(jì)時(shí)結(jié)束后跳轉(zhuǎn)頁面功能的相關(guān)資料,需要的朋友可以參考下。2017-11-11Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果
這篇文章主要介紹了Android 實(shí)現(xiàn)為點(diǎn)擊事件添加震動(dòng)效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android嵌套滾動(dòng)與協(xié)調(diào)滾動(dòng)的實(shí)現(xiàn)方式匯總
如何實(shí)現(xiàn)這種協(xié)調(diào)滾動(dòng)的布局呢,我們使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior實(shí)現(xiàn),另一種方案是MotionLayout,我們看看都是怎么實(shí)現(xiàn)的吧2022-06-06Android應(yīng)用開發(fā)中觸摸屏手勢(shì)識(shí)別的實(shí)現(xiàn)方法解析
這篇文章主要介紹了Android應(yīng)用開發(fā)中觸摸屏手勢(shì)識(shí)別的實(shí)現(xiàn)方法解析,深入的部分則是對(duì)左右手勢(shì)的識(shí)別給出了相關(guān)編寫思路,需要的朋友可以參考下2016-02-02Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解
這篇文章主要介紹了Android開發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下2016-02-02Android App中各種數(shù)據(jù)保存方式的使用實(shí)例總結(jié)
這篇文章主要介紹了Android App中各種數(shù)據(jù)保存方式的使用實(shí)例,列舉了SharedPreferences接口、機(jī)身空間存儲(chǔ)、SD卡存儲(chǔ)和SQLite數(shù)據(jù)庫四種方式的代碼例子,需要的朋友可以參考下2016-04-04android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼
這篇文章主要介紹了android實(shí)現(xiàn)短按電源鍵關(guān)機(jī)的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Android ViewPager實(shí)現(xiàn)無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)無限循環(huán)效果的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03