【问题标题】:How to add an orange line under the imageView image programatically?如何以编程方式在 imageView 图像下添加橙色线?
【发布时间】:2011-08-06 05:51:38
【问题描述】:

如何在图片显示在画廊之前在图片下方添加一条橙色线?
我想在图片上做标记,让它从其他图片中脱颖而出。

我已经测试了各种 LayoutParams,但需要建议。
仅在 xml 中查看有关如何执行此操作的大量说明。
这是我在适配器中的getView

(如果有人需要,请使用可行的解决方案进行更新)
imageViewWithLine 是具有布尔值的自定义 imageView
阻止是否应该画线

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null){

       BitmapFactory.Options bf = new BitmapFactory.Options();
       bf.inSampleSize = 8; 
       Bitmap bitmap = BitmapFactory.decodeFile(files.get(position).getImagePath(),bf);
       ImageViewWithLine imageViewWithLine = new ImageViewWithLine(ctx, null);
       BitmapDrawable b = new BitmapDrawable(getResources(),bitmap);
       imageViewWithLine.setLayoutParams(new Gallery.LayoutParams(80, 70));
       imageViewWithLine.setScaleType(ImageView.ScaleType.FIT_XY);
       imageViewWithLine.setBackgroundResource(GalItemBg);
       imageViewWithLine.setBackgroundDrawable(b);
       convertView = imageViewWithLine;

    }

    if(files.get(position).addLine() == true){
       ((ImageViewWithLine)convertView).setLine(true);
    }else
    ((ImageViewWithLine)convertView).setLine(false);

    return convertView;

    }
}

【问题讨论】:

    标签: android imageview layoutparams


    【解决方案1】:

    您可以扩展 ImageView 类并创建自定义视图。在自定义视图中,您可以覆盖 onDraw 并以这种方式绘制橙色线。

    更新:

    这只是一个普通按钮,底部有一个橙色条。尺寸不准确,但它应该为您提供一个良好的起点。

    public class ButtonWithLine extends Button {
    
        public ButtonWithLine(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setColor(Color.rgb(255, 125, 0));
            paint.setStyle(Paint.Style.FILL);
    
            float height = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    
            canvas.drawRect(0, getHeight() - height, getWidth(), getHeight(), paint);
    
            super.onDraw(canvas);
        }
    }
    

    【讨论】:

    • 它正在工作,但我必须将图库滚动到视野之外并再次返回以显示该行。如果我在画廊中有 100 张图像,那么重绘所有图像只是为了显示一个图像行是没有效率的。有没有办法告诉画廊只在图像或其他东西上重新加载
    • 我更新了我的问题,希望看到一个解决方案如何让画廊只更新选定的视图
    • 现在通过在自定义图像视图上调用 invalidate() 使其工作。谢谢@zeonic
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多