选择图片上传,9张不一样的图片的删除和记录

本文介绍了一种自定义ImageView的方法及实现多图片头像布局的技巧。通过继承ImageView类并覆盖绘制方法,可以轻松实现边框显示效果。此外,还提供了一个自定义的HeadImage布局,用于展示多个圆形头像,并通过不同位置参数来排列这些头像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值