如何在非Activity类的自定义视图中绘制位图

时间:2015-04-20 23:35:29

标签: java android bitmap

我创建了一个名为DrawViewSource的自定义视图,该视图扩展了View

   Paint paint;
    Location location;

    public DrawViewSource(Context context) {
        super(context);
        init();
    }

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

    }

    public DrawViewSource(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        paint = new Paint();
//        paint.setColor(Color.YELLOW);
//        paint.setStrokeWidth(10);
    }

    @Override
    public void onDraw(Canvas canvas) {

        Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.source_icon);
        canvas.drawBitmap(image, location.position_x, location.position_y, paint);

    }
}

canvas.drawBitmap(...)出错。我看到像onDraw()这样的实现,作者说它有效,我读到这个实现只能在Activity类中完成。我现在很困惑。

不介意课程Location,它没有错误。

我错过了一行代码吗?该怎么做,请开导我。

修改

04-21 07:49:58.938  26707-26707/com.myapp.alphasickbro.roomfindr E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myapp.alphasickbro.roomfindr, PID: 26707
    java.lang.NullPointerException
            at com.myapp.alphasickbro.roomfindr.DrawViewSource.onDraw(DrawViewSource.java:47)
            at android.view.View.draw(View.java:14740)
            at android.view.View.getDisplayList(View.java:13636)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.draw(View.java:14743)
            at android.view.View.getDisplayList(View.java:13636)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.draw(View.java:14743)
            at android.widget.FrameLayout.draw(FrameLayout.java:472)
            at android.widget.HorizontalScrollView.draw(HorizontalScrollView.java:1838)
            at android.view.View.getDisplayList(View.java:13636)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.draw(View.java:14743)
            at android.widget.FrameLayout.draw(FrameLayout.java:472)
            at android.widget.ScrollView.draw(ScrollView.java:1831)
            at android.view.View.getDisplayList(View.java:13636)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.getDisplayList(View.java:13631)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1060)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.getDisplayList(View.java:13631)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.draw(View.java:14743)
            at android.widget.FrameLayout.draw(FrameLayout.java:472)
            at android.view.View.getDisplayList(View.java:13636)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.getDisplayList(View.java:13631)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.getDisplayList(View.java:13631)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.getDisplayList(View.java:13631)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.View.draw(View.java:14457)
            at android.view.ViewGroup.drawChild(ViewGroup.java:3117)
            at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2954)
            at android.view.View.draw(View.java:14743)
            at android.widget.FrameLayout.draw(FrameLayout.java:472)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2329)
            at android.view.View.getDisplayList(View.java:13636)
            at android.view.View.getDisplayList(View.java:13678)
            at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
            at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
            at android.view.ViewRootImpl.draw(ViewRootImpl.java:2408)
            at android.view.ViewRootImpl.

我在这里创建Location()

Location loc;

    public CustomOnItemSelectedListener(MainActivity mac) {
        this.mac=mac;
    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        if (pos == 0) {
            loc= new Location(1322,227);
            mac.dvs.invalidate();
        }

//        Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
    }

这就是我的Location课程的样子:

public float position_x,position_y;

public Location(float position_x, float position_y) {
    this.position_x = position_x;
    this.position_y = position_y;
}

2 个答案:

答案 0 :(得分:0)

您的getResources()方法中可能是空指针异常。以下是在自定义视图中绘制位图的可行示例。

public class DrawViewSource extends View {

private Paint mPaint;
private Resources resources;
private Bitmap bitmap;

public DrawViewSource(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
    init(context, null, 0);
}

public DrawViewSource(Context context, AttributeSet attrs) {
    // TODO Auto-generated constructor stub
    super(context, attrs);
    init(context, attrs, 0);
}

public DrawViewSource(Context context, AttributeSet attrs, int defStyleAttr) {
    // TODO Auto-generated constructor stub
    super(context, attrs, defStyleAttr);
    init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
    // TODO Auto-generated method stub
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    resources = context.getResources();
    bitmap = BitmapFactory
            .decodeResource(resources, R.drawable.ic_launcher);
}

@SuppressLint("NewApi")
public DrawViewSource(Context context, AttributeSet attrs,
        int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    // TODO Auto-generated constructor stub
}

/*
 * (non-Javadoc)
 * 
 * @see android.view.View#onDraw(android.graphics.Canvas)
 */
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    canvas.drawBitmap(bitmap, 0, 0, mPaint);
}
}

并在您的布局中使用上面的自定义视图,如下所示。

<com.example.DrawViewSource
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

答案 1 :(得分:0)

因为你没有确切的位置,你调试了吗?

  

(location.position_x,location.position_y)

设置调试点:

  

canvas.drawBitmap(image,location.position_x,location.position_y,paint);   然后在调试模式下运行app。当代码运行到上面的行时,然后将鼠标移动到所有变量上以检查哪里为null?