先求一個windows版本的gif制作工具!!!
這個代碼只是做了簡單的選擇功能,圖片的壓縮與展示沒做。可以自己在接口的方法中去實現。
寫這個自定義view的初衷是為了學習LayoutParams,參考博客:http://www.jianshu.com/p/138b...
自定義一些屬性
<declare-styleable name="PhotoSelector"><attr name="columns" format="integer"/>//列<attr name="horizontal_space" format="dimension"/>//水平分割寬度<attr name="vertical_space" format="dimension"/>//豎直分割寬度</declare-styleable>
在布局中使用
<com.idealcn.define.view.view.PhotoSelectorandroid:layout_width="match_parent"android:layout_height="wrap_content"ideal:columns="3"ideal:horizontal_space="3dp"ideal:vertical_space="3dp"><!-- 默認添加一個添加按鈕的圖片 --><ImageViewandroid:layout_width="100dp"android:layout_height="100dp"android:src="@drawable/ic_suggestions_add"/></com.idealcn.define.view.view.PhotoSelector>
自定義這個ViewGroup
public class PhotoSelector extends ViewGroup {public PhotoSelector(Context context) {this(context,null);}public PhotoSelector(Context context, AttributeSet attrs) {this(context, attrs,0);}public PhotoSelector(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}
}
獲取默認的添加按鈕
@Overrideprotected void onFinishInflate() {super.onFinishInflate();//獲取默認的添加按鈕addView = getChildAt(0);viewList.add(addView);}
為添加按鈕添加點擊事件
@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();addView.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {//這里添加選擇圖片的代碼 }});}
子View的測量和布局
這里要重寫onMeasure和onLayout。定義ViewGroup.LayoutParams的子類來保存view的left和top的屬性。
public static class PhotoParams extends LayoutParams{public int left,top;public PhotoParams(Context c, AttributeSet attrs) {super(c, attrs);}public PhotoParams(int width, int height) {super(width, height);}public PhotoParams(LayoutParams source) {super(source);}}
測量時,view的寬高一致,屏幕寬度減去水平分割線的寬度,然后平分給每個view。
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = MeasureSpec.getSize(widthMeasureSpec);int childWidth = (int) ((width - horizontalSpace * columns)/columns + .5f);int childHeight = childWidth;int childCount = viewList.size();for (int x = 0; x < childCount; x++) {//這里要從保存view的集合中取出viewView child = viewList.get(x);PhotoParams lp = (PhotoParams) child.getLayoutParams();if (lp==null) {lp = new PhotoParams(childWidth, childHeight);}lp.width = childWidth;lp.height = childHeight;//這里確定了view的left和top屬性值,在onLayout中只需取出即可lp.left = x%columns * childWidth + (x%columns+1) * horizontalSpace;lp.top = x/columns * (childHeight + verticalSpace) + verticalSpace;child.setLayoutParams(lp);}if (childCount<=columns)setMeasuredDimension(childWidth*childCount,childHeight);else {int heightCount = childCount%columns!=0?(childCount/columns+1):childCount/columns;setMeasuredDimension(childWidth*columns,childHeight*heightCount);}//測量并確定子view的大小measureChildren(widthMeasureSpec,heightMeasureSpec);}
放置子view
@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int childCount = viewList.size();for (int x = 0; x < childCount; x++) {View child = viewList.get(x);PhotoParams lp = (PhotoParams) child.getLayoutParams();child.layout(lp.left,lp.top,lp.left + child.getMeasuredWidth() + horizontalSpace,lp.top + child.getMeasuredHeight() + verticalSpace);}}
添加圖片
定義一個List集合,添加的圖片存放于集合中。然后調用requestLayout()。定義一個接口,將添加圖片的操作交給所在的Activity處理。
定義接口
public interface OnAddPhotoListener {Bitmap addByCamera();//拍照Bitmap addByGallery();//相冊
}
所在的Activity實現接口
public class TableActivity extends AppCompatActivity implements OnAddPhotoListener {...//省略一部分代碼//只要把獲取圖片的操作在這里做處理即可@Overridepublic Bitmap addByCamera() {Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round);return bitmap;}@Overridepublic Bitmap addByGallery() {Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round);return bitmap;}
}
源碼參考:https://github.com/idealcn/De...