为什么我的抽签只有50%被抽出?

时间:2010-08-19 14:54:25

标签: android drawable

我尝试创建一个简单的应用程序,在触摸屏幕时绘制png图形。我遇到的问题是,实际上只渲染了我添加的图形的每个其他实例。每次添加新实例时,模式都会交替显示。我总是只获得50%的图形渲染。

我在HTC Hero上使用android级别3。

为了解释这个问题,这是我从表面持有者回调中调用的绘制方法:

 public void surfaceCreated(SurfaceHolder holder) {
  drawOK = true;
  drawIcon(100.0f, 100.0f);//line-1
  drawIcon(50.0f, 100.0f);//line-2
  drawIcon(100.0f, 50.0f);//line-3
  drawIcon(50.0f, 50.0f);//line-4
  //drawIcon(200.0f, 200.0f);//line-5
  //drawIcon(100.0f, 200.0f);//line-6
 }

当我有第5行和第5行时6注释掉(如图所示),我只看到第2行& 4呈现。当我在第5行添加时,我看到第1,3行和第1行。 5呈现。最后,当我在第6行添加时,我看到第2,4行和第2行。 6呈现。

为什么会这样?

更详细地解释其余的应用程序。我有以下布局xml:

<SurfaceView android:id="@+id/SurfaceView01" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"/>

即。我有一个单独的表面视图,我将绘制png图形。

我的活动实现了SurfaceHolder.Callback和intialises,如下所示:

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

    setContentView(R.layout.main);

    surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);

    // register our interest in hearing about changes to our surface
    holder = surfaceView.getHolder();
    holder.addCallback(this);
}

即。我链接调用,并使用R类设置内容视图。然后,我获得对表面视图的引用,并将活动注册为表面持有者回调。

我有一个方法可以将图标绘制到屏幕上。就是这样:

private void drawIcon(float x, float y) {
  if (!drawOK) return;
  Canvas canvas = holder.lockCanvas();
         // getting instance each time in case problem is related to reusing obj
         mFiringImage = getResources().getDrawable(R.drawable.icon);
         //so i can see overlapping icons, to rule out clipping issues
      mFiringImage.setAlpha(125); 
         // This mutate call is incase the bounds are shared state, which i doubt
      mFiringImage.mutate().setBounds( 
       (int)x-mFiringImage.getIntrinsicWidth()/2, 
       (int)y-mFiringImage.getIntrinsicHeight()/2, 
       (int)x+mFiringImage.getIntrinsicWidth()/2, 
       (int)y+mFiringImage.getIntrinsicHeight()/2
      );
      mFiringImage.draw(canvas);
      holder.unlockCanvasAndPost(canvas);
 }

即。我锁定画布,获取可绘制的png资源,设置边界(使用mutate以防万一),并要求drawable在画布上绘制自己。注意除非已创建曲面,否则我不会绘制。

我真的很困惑。

所有帮助表示赞赏。

P.S。以下是完整列表:

package com.mypackage;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Toucher extends Activity implements SurfaceHolder.Callback {
 private Drawable mFiringImage;
 private SurfaceView surfaceView;
 private boolean drawOK;
 private SurfaceHolder holder;

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

  setContentView(R.layout.main);

  surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);

  // register our interest in hearing about changes to our surface
  holder = surfaceView.getHolder();
  holder.addCallback(this);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  float x = event.getX();
  float y = event.getY();
  Log.e(getClass().getSimpleName(), "=========================");
  Log.e(getClass().getSimpleName(), "Action="+event.getAction());
  Log.e(getClass().getSimpleName(), "History Size="+event.getHistorySize());
  Log.e(getClass().getSimpleName(), "Pressure="+event.getPressure());
  Log.e(getClass().getSimpleName(), "Raw X="+event.getRawX());
  Log.e(getClass().getSimpleName(), "Raw Y="+event.getRawY());
  Log.e(getClass().getSimpleName(), "X="+x);
  Log.e(getClass().getSimpleName(), "Y="+y);
  Log.e(getClass().getSimpleName(), "Precision X="+event.getXPrecision());
  Log.e(getClass().getSimpleName(), "Precision Y="+event.getYPrecision());
  Log.e(getClass().getSimpleName(), "Size="+event.getSize());
  Log.e(getClass().getSimpleName(), "=========================");

  drawIcon(x, y);

  return super.onTouchEvent(event);
 }

 private void drawIcon(float x, float y) {
  if (!drawOK) return;
  Canvas canvas = holder.lockCanvas();
  mFiringImage = getResources().getDrawable(R.drawable.icon);
  mFiringImage.setAlpha(125);
  mFiringImage.mutate().setBounds(
    (int)x-mFiringImage.getIntrinsicWidth()/2, 
    (int)y-mFiringImage.getIntrinsicHeight()/2, 
    (int)x+mFiringImage.getIntrinsicWidth()/2, 
    (int)y+mFiringImage.getIntrinsicHeight()/2
  );
  mFiringImage.draw(canvas);
  holder.unlockCanvasAndPost(canvas);
 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
   int height) {
 }

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
  drawOK = true;
  drawIcon(100.0f, 100.0f);//1
  drawIcon(50.0f, 100.0f);//2
  drawIcon(100.0f, 50.0f);//3
  drawIcon(50.0f, 50.0f);//4
  //drawIcon(200.0f, 200.0f);//5
  //drawIcon(100.0f, 200.0f);//6
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);

  if (!hasFocus) drawOK = false;
 }
}

0 个答案:

没有答案