android HorizontalScrollView替代Gallery

本文转自:http://www.apkbus.com/forum.php?mod=viewthread&tid=72694

以前我们展示图片,很多时候都用Gallery,但是Gallery每次切换图片时都要新建视图,造成太多的资源浪费
所以细心的同志们可能已发现,当新建项目的API最低为8,其实Gallery这个类已经过期。
曾经大家的最爱的android源生的Gallery类因为自身原因已被放弃了,你是否还在用?
The type Gallery is deprecated.This widget is no longer supported. 
ther horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.
官方sdk文档提示用HSV和VP代替
但是我一直没有找到使用HorizontalScrollview来解决问题的实例
自已花了一个下午,整出实例来,希望对同志们有帮助,也欢迎讨论。

图片中使用了两个HorizontalScrollView,排成上下两行,每行均类似于之前gallery的效果
数据源一行在App的drawable包中,一行在sdcard中,sdk中的图片要自己找了拷进去,你懂的
附件是源码


布局文件如下:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <HorizontalScrollView   
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" >  
  10.         <LinearLayout   
  11.             android:orientation="horizontal"  
  12.             android:id="@+id/mygallery"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             />  
  16.     </HorizontalScrollView>  
  17.       
  18.     <HorizontalScrollView   
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content" >  
  21.         <LinearLayout   
  22.             android:orientation="horizontal"  
  23.             android:id="@+id/yourgallery"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             />  
  27.     </HorizontalScrollView>  
  28. </LinearLayout>  

activity代码如下

[java]  view plain copy
  1. package com.example.HSVdemo;  
  2.   
  3. import java.io.File;  
  4.   
  5. import com.example.galleydemo.R;  
  6.   
  7. import android.os.Bundle;  
  8. import android.os.Environment;  
  9. import android.app.Activity;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.graphics.BitmapFactory.Options;  
  13. import android.util.Log;  
  14. import android.view.Gravity;  
  15. import android.view.Menu;  
  16. import android.view.View;  
  17. import android.view.ViewGroup.LayoutParams;  
  18. import android.widget.ImageView;  
  19. import android.widget.ImageView.ScaleType;  
  20. import android.widget.LinearLayout;  
  21. import android.widget.Toast;  
  22. /** 
  23.  *  HorizontalScrollView in place of the Type Gallery 
  24.  * @author Administrator 
  25.  * 
  26.  */  
  27. public class HSVDemoActivity extends Activity {  
  28.       
  29.     private static final String tag="GH";  
  30.     private static final String path="/mnt/sdcard";  
  31.     private LinearLayout myGallery;  
  32.     private LinearLayout yourGallery;  
  33.   
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_hsv_demo);  
  38.           
  39.         myGallery=(LinearLayout) findViewById(R.id.mygallery);  
  40.         yourGallery=(LinearLayout) findViewById(R.id.yourgallery);  
  41.           
  42.         //pic in the drawable  
  43.         Integer[] images = { R.drawable.img0001, R.drawable.img0030,  
  44.                 R.drawable.img0100, R.drawable.img0130, R.drawable.img0200,  
  45.                 R.drawable.img0230, R.drawable.img0300, R.drawable.img0330,  
  46.                 R.drawable.img0354 };  
  47.           
  48.         for(Integer id:images){  
  49.             myGallery.addView(insertImage(id));  
  50.         }  
  51.           
  52.   
  53.         //pic in the sdcard  
  54.         String targetPath=path+"/test/";  
  55.         Log.d(tag,targetPath);  
  56.         Toast.makeText(getApplicationContext(), targetPath,Toast.LENGTH_LONG).show();  
  57.           
  58.         File targetDirector=new File(targetPath);  
  59.         File[] files=targetDirector.listFiles();  
  60.         Log.d(tag,files.length+"");  
  61.         for(File file:files){  
  62.             yourGallery.addView(insertPhoto(file.getAbsolutePath()));  
  63.         }  
  64.           
  65.     }  
  66.   
  67.     private View insertImage(Integer id) {  
  68.         LinearLayout layout=new LinearLayout(getApplicationContext());  
  69.         layout.setLayoutParams(new LayoutParams(320,320));  
  70.         layout.setGravity(Gravity.CENTER);  
  71.           
  72.         ImageView imageView=new ImageView(getApplicationContext());  
  73.         imageView.setLayoutParams(new LayoutParams(300,300));  
  74.         imageView.setBackgroundResource(id);  
  75.           
  76.         layout.addView(imageView);  
  77.         return layout;  
  78.     }  
  79.   
  80.   
  81.     private View insertPhoto(String absolutePath) {  
  82.         // TODO Auto-generated method stub  
  83.         Bitmap bm=decodeSampleBitmapFromUri(absolutePath,200,200);  
  84.         LinearLayout layout=new LinearLayout(getApplicationContext());  
  85.         layout.setLayoutParams(new LayoutParams(250,250));  
  86.         layout.setGravity(Gravity.CENTER);  
  87.           
  88.         ImageView imageView=new ImageView(getApplicationContext());  
  89.         imageView.setLayoutParams(new LayoutParams(220,220));  
  90.         imageView.setScaleType(ScaleType.CENTER_CROP);  
  91.         imageView.setImageBitmap(bm);  
  92.           
  93.         layout.addView(imageView);  
  94.         return layout;  
  95.     }  
  96.   
  97.   
  98.     private Bitmap decodeSampleBitmapFromUri(String absolutePath, int reqWidth, int reqHeight) {  
  99.         // TODO Auto-generated method stub  
  100.         Bitmap bm=null;  
  101.           
  102.         // First decode with inJustDecodeBounds=true to check dimensions  
  103.         final Options options=new Options();  
  104.         options.inJustDecodeBounds=true;  
  105.         BitmapFactory.decodeFile(absolutePath,options);  
  106.           
  107.         // Calculate inSampleSize  
  108.         options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);  
  109.           
  110.         // Decode bitmap with inSampleSize set  
  111.         options.inJustDecodeBounds=false;  
  112.         bm=BitmapFactory.decodeFile(absolutePath,options);  
  113.         return bm;  
  114.     }  
  115.   
  116.   
  117.     private int calculateInSampleSize(Options options, int reqWidth,  
  118.             int reqHeight) {  
  119.         // TODO Auto-generated method stub  
  120.         // Raw height and width of image  
  121.         final int height=options.outHeight;  
  122.         final int width=options.outWidth;  
  123.         int inSampleSize=1;  
  124.           
  125.         if(height>reqHeight||width>reqWidth){  
  126.             if(width>height){  
  127.                 inSampleSize=Math.round((float)height/(float)reqHeight);  
  128.             }else{  
  129.                 inSampleSize = Math.round((float)width / (float)reqWidth);  
  130.             }  
  131.         }  
  132.         return inSampleSize;  
  133.     }  
  134.   
  135. }  

在使用HorizontalScrollView时需注意,它必须有一个子控件用于显示,在本文中是线性布局,在该线性布局中添加View即可。。。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值