为什么我动态创建的图像视图不会分配点击事件

时间:2010-09-09 16:19:06

标签: java android

我有一个动态创建图像视图的for循环然后我为imageview分配onclicklistener,我已经分配了这些onclicklisteners但事件不会触发。为什么是这样?这是代码:

注意:BlockState由ImageView类扩展。创建ImageViews(BlockState)的位置在CreateLevelStorage方法(int级别)中。

package com.trik.pushover;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.*;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.os.*;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

public class PushOverView extends SurfaceView implements SurfaceHolder.Callback{

private PushOverThread _PushOverThread; 
/*private Levels _Level;*/
/*private final int _BlockHeight = 25;
private final int _BlockWidth = 6;*/
private ArrayList _ArrayList;
private final int _BlockGap = 10;
private Context _Context;

public PushOverView(Context context, AttributeSet attrs)
{
    super(context,attrs);
    SurfaceHolder holder = this.getHolder();
    holder.addCallback(this);
    this._Context = context;

    /*this._Level = new Levels(1,context);*/

    this._PushOverThread = new PushOverThread(holder,context, new Handler(),this);

}

public void surfaceCreated(SurfaceHolder holder)
{
    this.CreateLevelStorage(1);
    this._PushOverThread.start();
}

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

}

public void surfaceDestroyed(SurfaceHolder holder)
{

}

public void onDraw(Canvas canvas)
{
    //super.onDraw(canvas);
    this.DrawLevel1(canvas);
}

public void CreateLevelStorage(int level)
{
    if(level == 1 && this._ArrayList == null)
    {
    this._ArrayList = new ArrayList();

    BlockState blockstate1 = new BlockState(this._Context,R.drawable.standard);
    this._ArrayList.add(blockstate1);
    blockstate1.setOnClickListener(btnDoneOnClick);

    this._ArrayList.add(null);

    BlockState blockState2 = new BlockState(this._Context,R.drawable.standard);
    this._ArrayList.add(blockState2);
    blockState2.setOnClickListener(btnDoneOnClick);

    BlockState blockState3 = new BlockState(this._Context,R.drawable.standard);
    this._ArrayList.add(blockState3);
    blockState3.setOnClickListener(btnDoneOnClick);

    this._ArrayList.add(null);

    BlockState blockState4 = new BlockState(this._Context,R.drawable.standard);
    this._ArrayList.add(blockState4);
    blockState4.setOnClickListener(btnDoneOnClick);

    BlockState blockState5 = new BlockState(this._Context,R.drawable.standard);
    this._ArrayList.add(blockState5);
    blockState5.setOnClickListener(btnDoneOnClick);
    }
}

public void DrawLevel1(Canvas canvas)
{
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.BLUE);

    int x = 50;
    int y = 50;
    int width = 100;
    int height = 10;
    canvas.drawRect(new RectF(50,y,50+x,y+height),paint);

    for(int i = 0; i < this._ArrayList.size(); i++)
    {
    if(this._ArrayList.get(i) != null)
    {
        BlockState blockState = (BlockState) this._ArrayList.get(i);
        blockState.SetX(x);
        blockState.SetY(y-blockState.GetImage().getHeight());
        blockState.draw(canvas);

        x += this._BlockGap + 6;
    }
    else
    {
        x += 6 + this._BlockGap;
    }
    }

}




private final OnClickListener btnDoneOnClick = new OnClickListener() {
    public void onClick(View v) {
    Log.d("some_identifying_log_string", "Clicked");
      }
};



public class PushOverThread extends Thread
{
    private SurfaceHolder _Holder;
    private Canvas _Canvas;
    private Context _Context;
    private PushOverView _PushOverView;

    public PushOverThread(SurfaceHolder holder, Context context, Handler handler, PushOverView pushOverView)
    {
    this._Holder = holder;
    this._Context = context;
    this._PushOverView = pushOverView;

    }

    public void run()
    {
    while(true)
    {
        //Canvas c = null;
        try
        {
        this._Canvas = this._Holder.lockCanvas(null);

        this._PushOverView.onDraw(this._Canvas);
        //doDraw(this._Canvas);
        }
        finally 
        {
        if (this._Canvas != null) 
        {
            this._Holder.unlockCanvasAndPost(this._Canvas);
        }
        }
    }
    }

    /*public void doDraw(Canvas canvas)
    {
    //this._Level.doDraw();
    }*/
}
}

package com.trik.pushover;

import com.trik.pushover.R;
import android.widget.*;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;

public class BlockState extends ImageView {

 private int _x;
 private int _y;
 private Context _Context;
 private int _Drawable;
 private Bitmap _Image;
 private boolean _RotateImage = false;

 public BlockState(Context context, int drawable)
 {
  super(context);
  this._Context = context;
  this._Drawable = drawable;
  this._Image = BitmapFactory.decodeResource(this._Context.getResources(), R.drawable.standard);
 }

 public void onDraw(Canvas canvas)
 {
  super.onDraw(canvas);

 if(this._RotateImage)
 {
   canvas.save();
   canvas.rotate(60, this._x+6,this._y+25);
   canvas.drawBitmap(this._Image, this._x, this._y, null);
  canvas.restore();
 }
 else
 {
   canvas.drawBitmap(this._Image, this._x, this._y, null);
 }
 }

 public void SetRotateIamge(boolean isRotate)
 {
  this._RotateImage = isRotate;
 }

 public void SetX(int x)
 {
  this._x = x;
 }

 public void SetY(int y)
 {
  this._y = y;
 }

 public Bitmap GetImage()
 {
  return this._Image;
 }

}

0 个答案:

没有答案