Android 将imageview动态添加到视图
我正在制作使用view类的android游戏。我没有使用布局。我所有的图像都是用画布绘制的。现在我的问题是我不能给出位图 如您所知,可触摸事件。我正在尝试将imageview动态添加到我的view类以 使用可触摸事件。为什么尝试动态添加imageview?因为我没有使用 布局。但我不能。有人帮忙吗Android 将imageview动态添加到视图,android,Android,我正在制作使用view类的android游戏。我没有使用布局。我所有的图像都是用画布绘制的。现在我的问题是我不能给出位图 如您所知,可触摸事件。我正在尝试将imageview动态添加到我的view类以 使用可触摸事件。为什么尝试动态添加imageview?因为我没有使用 布局。但我不能。有人帮忙吗 here is my code: package com.example.poolmaster; import java.util.ArrayList; import android.anno
here is my code:
package com.example.poolmaster;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private int cuePosx,cuePosy;
int cueHeight,cueWeight;
double rotatingAngle;
int height;
int wwidth;
int cueHeight2,cueWeight2;
Bitmap table,stick,raise;
ImageView button = new ImageView(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rotatingAngle=0;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
wwidth = displaymetrics.widthPixels;
cueHeight2=height/2;
cueWeight2=wwidth/2;
System.out.println("**************************************");
System.out.println("height " +height);
System.out.println("weight" +wwidth);
System.out.println("**************************************");
// Set generic layout parameters
// Modify this
GamePlay custom = new GamePlay(this);
setContentView(custom);
// setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if(ev.getAction() == MotionEvent.ACTION_MOVE){
//cuePosy+=10;
//System.out.println("xCORDİNATES!!!! " +ev.getX());
//System.out.println("yCORDİNATES!!!! " +ev.getY());
rotatingAngle=getAngle(ev.getX(), ev.getY());
System.out.println("Angle " +rotatingAngle);
}
if(ev.getAction()==MotionEvent.ACTION_DOWN){
System.out.println("****************** ACTİON DOWN ****************");
//cueHeight2+=10;
//cueWeight2+=20;
//cuePosy=320;
}
if(ev.getAction()==MotionEvent.ACTION_UP){
System.out.println("****************** ACTİON DOWN ****************");
//cueHeight2-=10;
//cueWeight2-=20;
//cuePosy=320;
}
return true;
}
private double getAngle(double xTouch, double yTouch) {
double theta;
theta= Math.toDegrees( Math.atan2( height/2-yTouch , xTouch-wwidth/2) );
return theta;
}
public class GamePlay extends View{
public GamePlay(Context context) {
super(context);
// TODO Auto-generated constructor stub
table=BitmapFactory.decodeResource(getResources(), R.drawable.bg);
table= Bitmap.createScaledBitmap(table, wwidth, height, true);
stick=BitmapFactory.decodeResource(getResources(), R.drawable.stick);
raise=BitmapFactory.decodeResource(getResources(), R.drawable.raise);
cueHeight=stick.getHeight();
System.out.println("ıstaka " +cueHeight);
cueWeight=stick.getWidth();
cuePosx=wwidth/2;
cuePosy=height-cueHeight-180;
}
@SuppressLint({ "DrawAllocation", "DrawAllocation" })
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Matrix matrix = new Matrix();
matrix.setTranslate(cueWeight2, cueHeight2);
matrix.postRotate((float)rotatingAngle,cueWeight2, cueHeight2); // anti-clockwise by 90 degrees
// create a new bitmap from the original using the matrix to transform the result
Bitmap rotatedBitmap = Bitmap.createBitmap(stick , 0, 0, stick .getWidth(), stick .getHeight(), matrix,false);
canvas.save(); //Save the position of the canvas.
canvas.restore();
//Rotate the canvas.
canvas.drawBitmap(table, 0, 0, null); //Draw the ball on the rotated canvas.
canvas.drawBitmap(stick, matrix,null);
canvas.drawBitmap(raise, 0, 0,null);
invalidate();
}
}
}
这用于将imageview添加到扩展活动类的布局中
这用于将imageview添加到扩展视图类的画布
最初,不能使用画布放置任何图像视图、编辑文本或按钮。相反,你必须画它。因此,创建一个自定义布局并使用画布绘制该布局
试试这个,它可能对你有帮助。在onDraw(…)
再看另一个例子:
它提供了另一种添加视图的方法,请使用下面的代码 用于动态创建ImageView
ImageView mImgView1 = new ImageView(this);
要添加到视图中,请在setContentView(自定义)之前编写以下代码代码>(如果游戏性是你的视图类)
是的,您可以将此图像添加到任何视图。这里的布局对象是什么(layout.draw)?你是说lL对象?不幸的是,它给出了异常OK现在没有异常,但屏幕上没有图像现在删除这行:lL.draw(canvas);我不确定,试试吧是的gameplay是我的视图类(public void gameplay extensed View)这样写的。但是没有像custom这样的方法。add@TunçDoğan那么请用谷歌搜索一下,因为我不知道如何在自定义视图类中添加add()方法。如果我的答案对您有帮助,那么请接受它,这样也会对其他人有帮助。
LinearLayout lL = new LinearLayout(context);
ImageView imgView = new ImageView(context);
imgView.setVisibility(View.VISIBLE);
lL.addView(imgView);
lL.measure(canvas.getWidth(), canvas.getHeight());
lL.layout(0, 0, canvas.getWidth(), canvas.getHeight());
// placing the edit text at specific co-ordinates:
//canvas.translate(0, 0);
layout.draw(canvas);
ImageView mImgView1 = new ImageView(this);
custom.add(mImgView1);