如何使用Universal-Image-Loader在ImageView上绘制新图层

时间:2013-02-28 11:17:52

标签: android android-imageview android-gridview ondraw universal-image-loader

我们在项目中使用通用图像加载器(https://github.com/nostra13/Android-Universal-Image-Loader)。我们用它来加载gridview上的图像。但是,我们希望在所有图像上都有新的透明层和文本描述。当我们覆盖onDraw方法时,我们有这个图层,但只有当我们滚动gridview时它才可见。

是否可以使用此库在ImageView上绘制新图层?我们注意到,对于新的Android设备> 14,代码按预期工作,但不是在模拟器上,而不是在以前版本的系统上。

ProductView.java

public class ProductView extends ImageView {
    private Product product;
    private Paint p = new Paint();
    private Context context;

    public ProductView(Context context, Product product) {
        super(context);
        setScaleType(ScaleType.FIT_CENTER);
        setAdjustViewBounds(true);
        setId(product.getProductId());
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 300));
        this.product = product;
        this.context = context;
    }


    @Override
    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);
        Integer width = (int)canvas.getWidth();
        Integer height = (int)canvas.getHeight();

        p.setColor(Color.BLACK);
        p.setAlpha(80);
        canvas.drawRect(0, (int)(height*0.73), width, height, p);
        p.setAlpha(100);
        p.setColor(Color.WHITE);
        p.setTextSize(16);
        p.setFakeBoldText(true);
        StaticLayout layout = new StaticLayout(product.getProductName(), new TextPaint(p), width-10,
                                        Layout.Alignment.ALIGN_NORMAL,(float)1.0, (float)0.0,
                                        true);
        canvas.translate(5, (int)(height*0.73));
        layout.draw(canvas);
        canvas.restore();
    }
}

ImageGridActivity.java

public class ImageGridActivity extends Activity implements SlideMenuInterface.OnSlideMenuItemClickListener {
[...]

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        [...]
        GridView gridView = (GridView) findViewById(R.id.gridviewAC);
        gridView.setAdapter(new ImageGridUniwersalAdapter(this, imageLoader, products.productList, options));
        gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, true, true));
    }
[...]   
}

ImageGridUniwersalAdapter.java

public class ImageGridUniwersalAdapter extends BaseAdapter {
    private DisplayImageOptions options;
    private final List<Product> products;
    public ImageLoader imageLoader;
    private Context context;

    public ImageGridUniwersalAdapter(Context context, ImageLoader imageLoader, List<Product> products, DisplayImageOptions options){
        this.options = options;
        this.products = products;
        this.imageLoader = imageLoader;
        this.context = context;
    }

    [...]

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Product tmpProd = products.get(position);

        ProductView pv = new ProductView(this.context, tmpProd);
        imageLoader.displayImage(
                SettingsHelper.SERVERURL+tmpProd.getImgThumbUri(),
                pv,
                options);

        return pv;
    }
}

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/gridviewAC"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:gravity="center"
          android:horizontalSpacing="4dip"
          android:numColumns="2"
          android:stretchMode="columnWidth"
          android:verticalSpacing="4dip"
          android:padding="4dip" />

0 个答案:

没有答案