1
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package com.lzy.ninegrid;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@@ -11,7 +14,7 @@ import java.io.Serializable;
|
||||
* 修订历史:
|
||||
* ================================================
|
||||
*/
|
||||
public class ImageInfo implements Serializable {
|
||||
public class ImageInfo implements Serializable, Parcelable {
|
||||
public String thumbnailUrl;
|
||||
public String bigImageUrl;
|
||||
public int imageViewHeight;
|
||||
@@ -19,6 +22,7 @@ public class ImageInfo implements Serializable {
|
||||
public int imageViewX;
|
||||
public int imageViewY;
|
||||
public int type;
|
||||
public String videoUrl;
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
@@ -86,6 +90,50 @@ public class ImageInfo implements Serializable {
|
||||
", imageViewX=" + imageViewX +
|
||||
", imageViewY=" + imageViewY +
|
||||
", type=" + type +
|
||||
", videoUrl='" + videoUrl + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeString(this.thumbnailUrl);
|
||||
dest.writeString(this.bigImageUrl);
|
||||
dest.writeInt(this.imageViewHeight);
|
||||
dest.writeInt(this.imageViewWidth);
|
||||
dest.writeInt(this.imageViewX);
|
||||
dest.writeInt(this.imageViewY);
|
||||
dest.writeInt(this.type);
|
||||
dest.writeString(this.videoUrl);
|
||||
}
|
||||
|
||||
public ImageInfo() {
|
||||
}
|
||||
|
||||
protected ImageInfo(Parcel in) {
|
||||
this.thumbnailUrl = in.readString();
|
||||
this.bigImageUrl = in.readString();
|
||||
this.imageViewHeight = in.readInt();
|
||||
this.imageViewWidth = in.readInt();
|
||||
this.imageViewX = in.readInt();
|
||||
this.imageViewY = in.readInt();
|
||||
this.type = in.readInt();
|
||||
this.videoUrl = in.readString();
|
||||
}
|
||||
|
||||
public static final Creator<ImageInfo> CREATOR = new Creator<ImageInfo>() {
|
||||
@Override
|
||||
public ImageInfo createFromParcel(Parcel source) {
|
||||
return new ImageInfo(source);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ImageInfo[] newArray(int size) {
|
||||
return new ImageInfo[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -32,7 +33,7 @@ public class NineGridView extends ViewGroup {
|
||||
private int gridWidth; // 宫格宽度
|
||||
private int gridHeight; // 宫格高度
|
||||
|
||||
private List<ImageView> imageViews;
|
||||
private List<NineGridViewWrapper> imageViews;
|
||||
private List<ImageInfo> mImageInfo;
|
||||
private NineGridViewAdapter mAdapter;
|
||||
|
||||
@@ -94,8 +95,8 @@ public class NineGridView extends ViewGroup {
|
||||
if (mImageInfo == null) return;
|
||||
int childrenCount = mImageInfo.size();
|
||||
for (int i = 0; i < childrenCount; i++) {
|
||||
ImageView childrenView = (ImageView) getChildAt(i);
|
||||
|
||||
NineGridViewWrapper childrenView = (NineGridViewWrapper) getChildAt(i);
|
||||
|
||||
int rowNum = i / columnCount;
|
||||
int columnNum = i % columnCount;
|
||||
int left = (gridWidth + gridSpacing) * columnNum + getPaddingLeft();
|
||||
@@ -103,14 +104,17 @@ public class NineGridView extends ViewGroup {
|
||||
int right = left + gridWidth;
|
||||
int bottom = top + gridHeight;
|
||||
childrenView.layout(left, top, right, bottom);
|
||||
|
||||
|
||||
if (mImageLoader != null) {
|
||||
childrenView.setType(mImageInfo.get(i).type);
|
||||
mImageLoader.onDisplayImage(getContext(), childrenView, mImageInfo.get(i).thumbnailUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 设置适配器 */
|
||||
/**
|
||||
* 设置适配器
|
||||
*/
|
||||
public void setAdapter(@NonNull NineGridViewAdapter adapter) {
|
||||
mAdapter = adapter;
|
||||
List<ImageInfo> imageInfo = adapter.getImageInfo();
|
||||
@@ -160,24 +164,27 @@ public class NineGridView extends ViewGroup {
|
||||
}
|
||||
}
|
||||
//修改最后一个条目,决定是否显示更多
|
||||
if (adapter.getImageInfo().size() > maxImageSize) {
|
||||
View child = getChildAt(maxImageSize - 1);
|
||||
if (child instanceof NineGridViewWrapper) {
|
||||
NineGridViewWrapper imageView = (NineGridViewWrapper) child;
|
||||
imageView.setMoreNum(adapter.getImageInfo().size() - maxImageSize);
|
||||
}
|
||||
}
|
||||
// if (adapter.getImageInfo().size() > maxImageSize) {
|
||||
// View child = getChildAt(maxImageSize - 1);
|
||||
// if (child instanceof NineGridViewWrapper) {
|
||||
// NineGridViewWrapper imageView = (NineGridViewWrapper) child;
|
||||
// imageView.setMoreNum(adapter.getImageInfo().size() - maxImageSize);
|
||||
// }
|
||||
// }
|
||||
|
||||
mImageInfo = imageInfo;
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
/** 获得 ImageView 保证了 ImageView 的重用 */
|
||||
private ImageView getImageView(final int position) {
|
||||
ImageView imageView;
|
||||
/**
|
||||
* 获得 ImageView 保证了 ImageView 的重用
|
||||
*/
|
||||
private NineGridViewWrapper getImageView(final int position) {
|
||||
NineGridViewWrapper imageView;
|
||||
if (position < imageViews.size()) {
|
||||
imageView = imageViews.get(position);
|
||||
} else {
|
||||
imageView = mAdapter.generateImageView(getContext());
|
||||
imageView = mAdapter.generateImageView(getContext(), mAdapter.getImageInfo().get(position).getType());
|
||||
imageView.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
@@ -189,22 +196,30 @@ public class NineGridView extends ViewGroup {
|
||||
return imageView;
|
||||
}
|
||||
|
||||
/** 设置宫格间距 */
|
||||
/**
|
||||
* 设置宫格间距
|
||||
*/
|
||||
public void setGridSpacing(int spacing) {
|
||||
gridSpacing = spacing;
|
||||
}
|
||||
|
||||
/** 设置只有一张图片时的宽 */
|
||||
/**
|
||||
* 设置只有一张图片时的宽
|
||||
*/
|
||||
public void setSingleImageSize(int maxImageSize) {
|
||||
singleImageSize = maxImageSize;
|
||||
}
|
||||
|
||||
/** 设置只有一张图片时的宽高比 */
|
||||
/**
|
||||
* 设置只有一张图片时的宽高比
|
||||
*/
|
||||
public void setSingleImageRatio(float ratio) {
|
||||
singleImageRatio = ratio;
|
||||
}
|
||||
|
||||
/** 设置最大图片数 */
|
||||
/**
|
||||
* 设置最大图片数
|
||||
*/
|
||||
public void setMaxSize(int maxSize) {
|
||||
maxImageSize = maxSize;
|
||||
}
|
||||
|
||||
@@ -32,11 +32,13 @@ public abstract class NineGridViewAdapter implements Serializable {
|
||||
* 如果需要自定义图片展示效果,重写此方法即可
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param type
|
||||
* @return 生成的 ImageView
|
||||
*/
|
||||
protected ImageView generateImageView(Context context) {
|
||||
protected NineGridViewWrapper generateImageView(Context context, int type) {
|
||||
NineGridViewWrapper imageView = new NineGridViewWrapper(context);
|
||||
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||
imageView.setType(type);
|
||||
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
|
||||
imageView.setImageResource(R.drawable.ic_default_color);
|
||||
return imageView;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.lzy.ninegrid;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
@@ -13,6 +15,7 @@ import android.util.TypedValue;
|
||||
import android.view.MotionEvent;
|
||||
import android.widget.ImageView;
|
||||
|
||||
|
||||
public class NineGridViewWrapper extends ImageView {
|
||||
|
||||
private int moreNum = 0; //显示更多的数量
|
||||
@@ -21,7 +24,8 @@ public class NineGridViewWrapper extends ImageView {
|
||||
private int textColor = 0xFFFFFFFF; //显示文字的颜色
|
||||
|
||||
private TextPaint textPaint; //文字的画笔
|
||||
private String msg = ""; //要绘制的文字
|
||||
private String msg = "哈哈哈啊"; //要绘制的文字
|
||||
private int mType;
|
||||
|
||||
public NineGridViewWrapper(Context context) {
|
||||
this(context, null);
|
||||
@@ -44,14 +48,28 @@ public class NineGridViewWrapper extends ImageView {
|
||||
textPaint.setColor(textColor); //设置文字颜色
|
||||
}
|
||||
|
||||
public void setType(int type) {
|
||||
mType = type;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
if (moreNum > 0) {
|
||||
canvas.drawColor(maskColor);
|
||||
float baseY = getHeight() / 2 - (textPaint.ascent() + textPaint.descent()) / 2;
|
||||
canvas.drawText(msg, getWidth() / 2, baseY, textPaint);
|
||||
}
|
||||
if (mType == 2) {
|
||||
// canvas.drawColor(maskColor);
|
||||
// float baseY = getHeight() / 2 - (textPaint.ascent() + textPaint.descent()) / 2;
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_image);
|
||||
canvas.translate(getWidth() / 2, getHeight() / 2);
|
||||
canvas.drawBitmap(bitmap, -50, -50, null);
|
||||
}
|
||||
// if (mType == 2) {
|
||||
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.phone);
|
||||
// // canvas.translate(getWidth() / 2, getHeight() / 2);
|
||||
// canvas.drawBitmap(bitmap, 0, 0, null);
|
||||
// }
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
BIN
ninegridview/src/main/res/drawable/icon_image.png
Normal file
BIN
ninegridview/src/main/res/drawable/icon_image.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.6 KiB |
Reference in New Issue
Block a user