gpt4 book ai didi

java - ImageView 未在RelativeLayout 中显示

转载 作者:行者123 更新时间:2023-12-01 15:39:59 25 4
gpt4 key购买 nike

我在制作 ImageView (dbm_needle) 时遇到问题,我想在按下按钮后使用下面的代码进行旋转,并在相对布局上正确显示。内容似乎正确地开始绘制,但按钮按下似乎没有启动使针旋转的 onTouch 事件,我似乎无法弄清楚出了什么问题。

counter.xml:

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dblmeter_screen"
android:gravity="center_horizontal">

Java Activity :

public class Counter extends Activity {

private boolean keepRotating = false;
private boolean returnRotating = true;
private Random mRnd = new Random();
private static final int INTERVAL = 25;
int degrees =-65;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.counter);
}

public void make(float x){
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.dblmeter_screen) ;

Bitmap rotate_needle = BitmapFactory.decodeResource(getResources(), R.drawable.dbm_needle);

int width = rotate_needle.getWidth();
int height = rotate_needle.getHeight();
int newWidth = 6;
int newHeight = 400;

float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
matrix.postRotate(x);

Bitmap resizedBitmap = Bitmap.createBitmap(rotate_needle, 0, 0,
width, height, matrix, true);

BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

ImageView imageView = new ImageView(this);

imageView.setImageDrawable(bmd);

imageView.setScaleType(ScaleType.CENTER);

setContentView(relLayout);
}

public void make(int degrees)
{
Log.i(this.getClass().toString(), "Rotation : " + degrees);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
startRotating();
break;
case MotionEvent.ACTION_UP:
stopRotating();
break;
}

return super.onTouchEvent(event);
}

public void startRotating()
{
returnRotating = false;
if (!keepRotating)
{
keepRotating = true;

final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if (keepRotating)
{
int Rand = mRnd.nextInt(30);
degrees = (degrees + (Rand/2)-Rand/6) % 360;
if(degrees > 65)
degrees = degrees - Rand;
make(degrees);

handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}

public void stopRotating()
{
keepRotating = false;
if (!returnRotating)
{
returnRotating = true;

final Handler handler = new Handler();

handler.postDelayed(new Runnable()
{
@Override
public void run()
{
if (returnRotating)
{
int Rand = mRnd.nextInt(30);
degrees = (degrees - (Rand/2)+Rand/6) % 360;
if(degrees < -65)
degrees = degrees + Rand;
make(degrees);

handler.postDelayed(this, INTERVAL);
}
}
}, INTERVAL);
}
}

public void pushClick(View pushClick) {
switch (pushClick.getId()) {
case R.id.btn_push:
make(degrees);
}

}

最佳答案

堆栈跟踪似乎显示内部错误。有时 eclipse 会无缘无故地把事情搞砸。尝试清理项目或重新启动 eclipse 或重新启动 adb。这可能会解决您的问题。

那么我认为你应该将布局参数添加到创建的 ImageView 中

imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT
,LayoutParams.WRAP_CONTENT));

关于java - ImageView 未在RelativeLayout 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8219596/

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