Hi,
I'm trying to create multiple ImageView's each with its own onTouchListener
that will redraw a bitmap on motion events. I dynamically create a new
object for each ImageView and this works fine but the onTouchListener seems
to only be tied to the last view created. I think this is because the
ImageViews are taking the entire screen and just overlaying on each other.
So while I can see the previous image I can't get to the onTouchListener for
it because everything is intercepted by the last ImageView laid down. If I
try to limit the size of the ImageView to the size of my Bitmap I can't move
the Bitmap outside of those boundaries and still see it on the screen. Can
anyone explain how I can get each 50 x 50 ImageView to appear on the screen,
each with its own onTouchListener?
Thanks!
Here's my main:
public class dominotest extends Activity {
private static final int FILL_PARENT = -1;
FrameLayout mainLayout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new FrameLayout(this);
LayoutParams mainLayoutParams = new LayoutParams(FILL_PARENT,
FILL_PARENT);
mainLayout.setLayoutParams(mainLayoutParams);
ImageView i = new ImageView(this);
ImageView j = new ImageView(this);
TileView stile = new TileView(i.getContext(), 1);
TileView stile2 = new TileView(j.getContext(), 2);
mainLayout.addView(stile);
mainLayout.addView(stile2);
setContentView(mainLayout);
}
}
Here's my TileView class:
public class TileView extends View {
public TileView(Context context, int dots) {
super(context);
initTile(dots);
// TODO Auto-generated constructor stub
}
private final Paint mPaint = new Paint();
private Bitmap myTile;
private int leftPos = 60;
private int topPos = 60;
public void initTile(int dots) {
setFocusable(true);
Resources r = this.getContext().getResources();
if ( dots == 1 ) {
loadTile(r.getDrawable(R.drawable.stile_1dot));
leftPos = 10;
topPos = 10;
}
else if ( dots == 2 ) {
loadTile(r.getDrawable(R.drawable.stile_2dots));
leftPos=50;
topPos = 50;}
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int x = (int)event.getX();
int y = (int)event.getY();
leftPos = x;
topPos = y;
TileView.this.invalidate();
return true;
}
});
}
public void loadTile(Drawable tile) {
int tileWidth = 50;
int tileHeight = 50;
Bitmap bitmap = Bitmap.createBitmap(tileWidth, tileHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tile.setBounds(0, 0, tileWidth, tileHeight);
tile.draw(canvas);
myTile = bitmap;
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(myTile, leftPos, topPos, mPaint);
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en