过期Gallery取代方案horizontalscrollView之普通画廊
activity_main.xml
-------------------------------------------------------------------------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- HorizontalScrollView必须包含子元素才能增加视图 -->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/mygallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
</LinearLayout>
MainActivity.java
-------------------------------------------------------------------------------------------------------------------------------
package com.horizontalscrollView;
import java.lang.reflect.Field;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
*
* 项目名称:com.horizontalscrollView
* 类 名 称:MainActivity
* 类 描 述:HorizontalScrollView代替gallery效果
* 创 建 人:fy
* 创建时间:2014-3-17 下午10:36:10
* Copyright (c) 方勇-版权所有
*/
public class MainActivity extends Activity {
private LinearLayout myGallery;
/* 图片ID集合 */
private static ArrayList<Integer> images = new ArrayList<Integer>();
/* 屏幕宽度(像素) */
private int width;
/* 屏幕高度(像素) */
private int height;
/* 屏幕密度(0.75 / 1.0 / 1.5) */
private float density;
/* 屏幕密度DPI(120 / 160 / 240) */
private int densityDpi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setImages();
setScreens();
findViews();
initdata();
}
private void findViews() {
myGallery = (LinearLayout) findViewById(R.id.mygallery);
}
private void initdata() {
for (Integer id : images) {
myGallery.addView(insertImage(id));
}
}
// 用反射机制来获取资源中的图片ID
private void setImages() {
Field[] fields = R.drawable.class.getDeclaredFields();
for (Field field : fields) {
if (!"icon".equals(field.getName())) {
int index = 0;
try {
index = field.getInt(R.drawable.class);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// 保存图片ID
images.add(index);
}
}
}
/* 手机屏幕属性 */
private void setScreens() {
// 定义DisplayMetrics 对象
DisplayMetrics metric = new DisplayMetrics();
// 取得窗口属性
getWindowManager().getDefaultDisplay().getMetrics(metric);
width = metric.widthPixels; // 屏幕宽度(像素)
height = metric.heightPixels; // 屏幕高度(像素)
density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)
}
private View insertImage(Integer id) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(width, height));
layout.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(width, height));
imageView.setBackgroundResource(id);
layout.addView(imageView);
return layout;
}
}
效果
-------------------------------------------------------------------------------------------------------------------------------
<!--EndFragment-->