在自定义图像视图中集成毕加索

时间:2017-04-29 07:51:44

标签: android android-custom-view picasso

我正在尝试构建自定义图像视图并在课堂内使用Picasso,以避免在活动代码中设置图像。 我尝试了不同的路径,但没有一条路可行。 这是我的自定义类

package com.mycompany.picassoimageview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.net.Uri;
import android.os.Build;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.io.IOException;

/**
 * TODO: document your custom view class.
 */
public class PicassoImageView extends ImageView {

    int imgsrc;
    Bitmap bmp;
    Paint pnt;
    Picasso pcs;
    public PicassoImageView(Context context) {
        super(context);
        init(context,null);

    }

    private void init(Context ctx, AttributeSet a) {
        pcs = Picasso.with(ctx);
        pnt = new Paint();
        pnt.setAntiAlias(true);
        if(a != null) {
            imgsrc = a.getAttributeResourceValue(R.styleable.PicassoImageView_msrc, 0);
            if (imgsrc != 0) {
                try {
                    bmp = pcs.load(imgsrc).get();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public PicassoImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
     }

    public PicassoImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);

        // TODO: consider storing these as member variables to reduce
        // allocations per draw cycle.
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();

        // Draw the text.
        if(bmp != null)
        {
            bmp.prepareToDraw();
            canvas.drawBitmap(bmp,(float)paddingLeft,(float)paddingTop,pnt);
        }

    }

    @Override
    public void setImageResource(int resId) {
        try {
            bmp = Picasso.with(getContext()).load(resId).get();
            invalidate();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setImageURI(Uri uri) {
        try {
            bmp = Picasso.with(getContext()).load(uri).get();
            invalidate();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

现在,问题在于init()方法,上下文确实存在,但 a.getAttributeResourceValue 总是无法返回data_icons.png的地址;因此,值始终为零, bmp 为空

这是属性文件:

<resources>
    <declare-styleable name="PicassoImageView">
        <attr name="msrc" format="integer" /> <!-- tried "reference" too -->
    </declare-styleable>
</resources>

这是使用视图的活动xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mycompany.picassoimageview.MainActivity">
    <com.mycompany.picassoimageview.PicassoImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#efff11"
        app:msrc="@drawable/data_icons"/>
</LinearLayout>

在活动课中没有任何事情发生:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

1 个答案:

答案 0 :(得分:1)

在阅读了几个样本后,我发现问题在于我是如何阅读这些属性的。而不是:

imgsrc = a.getAttributeResourceValue(R.styleable.PicassoImageView_msrc, 0);

我应该用过这个:

TypedArray ta = ctx.obtainStyledAttributes(a, R.styleable.PicassoImageViewClass);
int q = ta.getResourceId(R.styleable.PicassoImageViewClass_msrc,-1);
if(q>0)
   imgSrc = q;
ta.recycle();