要做一个这样的效果 GridView显示图片 (自做 相册)
GrdiView显示Pic 但是用imageLoader加载的时候出现一个问题 就是 当固定Item高度的时候会产生 Item很长 并且每一项的长度不同
于是想固定高度,但是要显示正方形图片,如果固定了高度,那适配肯定有问题。并且固定高度也不能保证宽高一样。
于是重写一个ViewGroup 在 onMeasure的时候使得这个ViewGroup的宽高相同 ,然后这个ViewGroup当作GridView的Item的父布局
public class SquareView extends RelativeLayout {
public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareView(Context context) {
super(context);
}
@SuppressWarnings("unused")
@Override
protected void onMeasure(int
widthMeasureSpec, int
heightMeasureSpec) {
// For simple implementation, or internal size is always 0.
// We depend on the container to specify the layout size of
// our view. We can't really know what it is since we will be
// adding and removing different arbitrary views and do not
// want the layout to change as this happens.
setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
getDefaultSize(0, heightMeasureSpec));
// Children are just made to fill our space.
int childWidthSize = getMeasuredWidth();
int childHeightSize = getMeasuredHeight();
// 高度和宽度一样
heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(
childWidthSize, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
在GridView的Item中布局中的引用
<?xml version="1.0" encoding="utf-8"?>
<View.SquareView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dip"
android:orientation="vertical" >
<ImageView
android:scaleType="fitXY"
android:id="@+id/img_grallyactivity_grdiviewitem"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</View.SquareView>
//-------------------------------------------------------------------------------------------------------
接下来是获取到相册的图片URL方便加载
ContentResolver cr = getContentResolver();
String columns[] = new String[] { Media._ID, Media.BUCKET_ID,
Media.PICASA_ID, Media.DATA, Media.DISPLAY_NAME, Media.TITLE,
Media.SIZE, Media.BUCKET_DISPLAY_NAME };
Cursor cur = cr.query(Media.EXTERNAL_CONTENT_URI, columns, null, null,
null);
if (cur.moveToFirst()) {
int photoPathIndex = cur.getColumnIndexOrThrow(Media.DATA);
do {
String path = cur.getString(photoPathIndex);
}while (cur.moveToNext());
}
cur.close();
使用内容提供者来获取到系统的图片的路径 然后用imageLoader进行加载
//----------------------------------------------------------------------------------------------------------
ImageLoader开始加载的时候出现了问题 显示加载不到资源
虽然我们通过contentProvider获取到的URL肯定是正确的 , 但是ImageLoaer加载不到 ,因为 ImageLoader 对不同的来源有自己的加载规则
①网络加载 URL的前面必须是 http://
②非网络加载 URL前面必须是 file://
所以在我们的URL前面加上 file:// 即可
听说assest的文件 URL前面必须是 assest:// -------->这一项当前还没试