gpt4 book ai didi

java - 绘制 ImageView

转载 作者:行者123 更新时间:2023-11-30 01:15:50 26 4
gpt4 key购买 nike

这个领域的新手,希望得到一些帮助。
我有一个"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();
}
}

感谢您的帮助!

最佳答案

我没有正确理解你的问题,但如果你必须将 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);

关于java - 绘制 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37877488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com