package com.gotye.vshow.view;
import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.widget.ImageView; import com.gotye.vshow.R; public class MyImageView extends ImageView { private int top; private int left; private int right; private int bottom; public final int SHOW=1; Paint paint; public MyImageView(Context context) { super(context); initPaint(); } public MyImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); } private void initPaint() { paint = new Paint(); paint.setAntiAlias(true); paint.setStrokeWidth(3f); paint.setColor(getResources().getColor(R.color.white)); paint.setStyle(Style.STROKE); } public void init(int top, int bottom, int left, int right) { this.top = top; this.bottom = bottom; this.left = left; this.right = right; } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (top == SHOW) { canvas.drawLine(0, 0, this.getWidth(), 0, paint); } if (bottom == SHOW) { canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); } if (left == SHOW) { canvas.drawLine(0, 0, 0, this.getHeight(), paint); } if (right == SHOW) { canvas.drawLine(this.getWidth(), 0, this.getWidth(), this.getHeight(), paint); } } }
package com.gotye.vshow.view; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.gotye.vshow.R; import java.util.ArrayList; public class HeadImage extends ViewGroup{ ArrayList<ImageView> ImageList=new ArrayList<>(); private Context mContext; public HeadImage(Context context) { super(context); this.mContext=context; } public HeadImage(Context context, AttributeSet attrs) { super(context, attrs); this.mContext=context; } public HeadImage(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext=context; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int mViewGroupWidth = getMeasuredWidth(); // 当前ViewGroup的总宽度 int mViewGroupHeight = getMeasuredHeight(); int count = getChildCount(); int mleft=0; int mright=0; for (int i = 0; i < count; i++) { View view = getChildAt(i); if (i == 0) { view.layout(0, 0, mViewGroupWidth/2, mViewGroupHeight*2/3); } if (i == 1) { view.layout(mViewGroupWidth/2, 0,mViewGroupWidth*3/4, mViewGroupHeight/3); } if (i == 2) { view.layout(mViewGroupWidth*3/4, 0,mViewGroupWidth, mViewGroupHeight/3); } if (i == 3) { view.layout(mViewGroupWidth/2,mViewGroupHeight/3,mViewGroupWidth*3/4, mViewGroupHeight*2/3); } if(i==4){ view.layout(mViewGroupWidth*3/4,mViewGroupHeight/3,mViewGroupWidth, mViewGroupHeight*2/3); } if(i>4){ mleft=mViewGroupWidth*(i-5)/4; mright=mViewGroupWidth*((i-4))/4; view.layout(mleft, mViewGroupHeight*2/3, mright, mViewGroupHeight); } } } public void setData(ArrayList<MyImageView> list){ ImageList.clear(); this.removeAllViews(); for (int i=0;i<9;i++){ MyImageView image; if(list!=null&& i<list.size()){ image=list.get(i); }else{ image=new MyImageView(mContext); image.setBackgroundColor(Color.rgb(240,239,245)); } if( i==8 && list==null||(list!=null && list.size()!=9 && i==8)){ image.setImageResource(R.mipmap.add_pic); } switch (i){ case 0: image.init(0,1,1,1); break; case 3: image.init(1,1,1,1); break; case 4: image.init(1,1,1,1); break; case 5: case 6: case 7: case 8: image.init(1,1,1,1); break; } final int pos=i; image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(mImageInterface!=null){ mImageInterface.imageClick(pos,(ImageView)v); } } }); ImageList.add(image); LayoutParams layoutParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); this.addView(ImageList.get(i),layoutParams); } invalidate(); } ImageInterface mImageInterface; public void setHeadImageLister(ImageInterface mImageInterface){ this.mImageInterface=mImageInterface; } public interface ImageInterface{ public void imageClick(int i ,ImageView imageView); } }