創作內容

1 GP

簡簡單單取代Gallery--horizontalscrollview用法

作者:武藤│2014-05-01 17:55:31│巴幣:2│人氣:2866
經過這幾天的爬文,總算解決Gallery廢棄的問題

利用horizontalscrollview+LinearLayout+ImageView這三種

做法就是在horizontalscrollview內塞一個LinearLayout,然後利用迴圈把ImageView全部塞入LinearLayout,成品如下圖所示



Activity原始碼:

package com.example.pj3;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher.ViewFactory;
import android.app.Activity;
import android.content.Context;

import com.example.pj3.R;

public class Anothergallery extends Activity implements ViewFactory {
    
    //原圖
        private Integer[] imgArr = {
                R.drawable.d1b, R.drawable.d2b, R.drawable.d3b, R.drawable.d4b,
                R.drawable.d5b, R.drawable.d6b, R.drawable.d7b, R.drawable.d8b,
                R.drawable.d9b, R.drawable.d10b, R.drawable.d11b, R.drawable.d12b,
                R.drawable.d13b, R.drawable.d14b, R.drawable.d15b, R.drawable.d16b
        };
        
        //縮圖
        private Integer[] thumbImgArr = {
                R.drawable.d1, R.drawable.d2, R.drawable.d3, R.drawable.d4,
                R.drawable.d5, R.drawable.d6, R.drawable.d7, R.drawable.d8,
                R.drawable.d9, R.drawable.d10, R.drawable.d11, R.drawable.d12,
                R.drawable.d13, R.drawable.d14, R.drawable.d15, R.drawable.d16
        };
        
        private LinearLayout linelay;
        private ImageSwitcher imgSwi2;
        
        
        
        
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.anothergallery);
            
            
            linelay = (LinearLayout)findViewById(R.id.anogallery);
            
            imgSwi2 = (ImageSwitcher)findViewById(R.id.imgSw2);
            imgSwi2.setFactory(this);
            imgSwi2.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
            imgSwi2.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
            
            
            
            for(int i=0; i < thumbImgArr.length; i++)
            {
                    linelay.addView(getImageView(i));
                    
            }
        }
        
        private ImageView getImageView(int i){
            ImageView img = new ImageView(this);
            img.setImageResource(thumbImgArr[i]);
            img.setId(i);
            img.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v)
                {
                    imgSwi2.setImageResource(imgArr[v.getId()]);
                    Toast.makeText(v.getContext(), "您選擇了"+(v.getId()+1), Toast.LENGTH_SHORT).show();
                }
            });
            
            return img;
        }
        
        
        public View makeView(){
            ImageView v1 = new ImageView(this);
            v1.setBackgroundColor(0xFF000000);
            v1.setScaleType(ImageView.ScaleType.FIT_CENTER);
            v1.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            return v1;
        }

}
============================分隔線==================================

配置檔:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   

    <ImageSwitcher
        android:id="@+id/imgSw2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ImageSwitcher>

    <HorizontalScrollView
        android:id="@+id/honScview"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/anogallery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>


引用網址:https://home.gamer.com.tw/TrackBack.php?sn=2426255
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

1喜歡★kyo123lala 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:placeholderf... 後一篇:猴子也知道路的全地圖村莊...

追蹤私訊切換新版閱覽

作品資料夾

ilove487奇幻小說連載中
《克蘇魯的黎明》0672.不用道歉看更多我要大聲說昨天15:06


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】