绘制ImageView

时间:2016-06-17 09:08:08

标签: java android

这个领域的新人,想借助一些帮助 我有一个“Missile.java”类,我在那里画画。我想画一个ImageView,我正在使用这段代码:

ImageView v = (ImageView) findViewById(R.id.missile);
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
    canvas.drawBitmap(b, 50,50,paint);

但它没有画出图像!...  “Missile.java” :(正在绘制其他内容)

package com.myfirstapplication.owner.appversion1;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by Owner on 16/06/2016.
 */
public class Missile
        extends ImageView {
    Board b;
    public Missile(Context context) {
        super(context);
        //   init(context);
    }

    public Missile(Board b, Context context, AttributeSet attrs) {
        super(context, attrs);
        this.b=b;
    }

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


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

    @TargetApi(21)
    public Missile(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        //init(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x = getWidth();
        float y = getHeight();
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.parseColor("#CD5C5C"));
        ImageView v = (ImageView) findViewById(R.id.missile);
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.missile_cartoon);
        Bitmap bNew = Bitmap.createScaledBitmap(b,250,250,true);
        canvas.drawBitmap(bNew, 50,50,paint);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        super.setOnTouchListener(l);
        b.addOneCoinsCounter();
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我无法正确理解您的问题,但如果您必须将R.drawable.missile_cartoon放入Imageview" v"那么

ImageView v = (ImageView) findViewById(R.id.missile);
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),R.drawable.missile_cartoon);
v.setImageBitmap(bitmap);