I have created two Bitmap animations on a SurfaceView. I want these Bitmaps to be clickable. It is my understanding from doing some research that you cannot add an OnClickListener to a Bitmap. I have therefore tried to add two ImageButtons in my XML file and the plan is to attach them to my Bitmaps with .setImageBitmap
The animations work fine but since trying to add the inflator to access my XML and OnClickListener everything has gone sideways.
I have just started programming and this is just a small project I've been working on. I'm not sure if the Imagebutton approach is wrong or not. I have been trying for days to get my animations to be clickable.
Any help is greatly appreciated
Logcat
error: incompatible types: playActivity.surfaceView cannot be converted to Context
Code
public class playActivity extends AppCompatActivity {
surfaceView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new surfaceView(this);
setContentView(view);
}
public class surfaceView extends SurfaceView {
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
View secondLayerView = LayoutInflater.from(this).inflate(R.layout.activity_play, null, false);
int[] purple_bird = {
R.drawable.bird1,
R.drawable.bird2
};
int[] red_bird = {
R.drawable.red1,
R.drawable.red2,
R.drawable.red3,
R.drawable.red4,
R.drawable.red5,
R.drawable.red6,
R.drawable.red7,
R.drawable.red8,
R.drawable.red9,
R.drawable.red10
};
ImageButton imageButton1 = (ImageButton) findViewById(R.id.imageButton);
ImageButton imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
public surfaceView(Context context) {
super(context);
new Anim().start();
imageButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Hello Red Bird",Toast.LENGTH_SHORT).show();
}
});
imageButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Hello Purple Bird",Toast.LENGTH_SHORT).show();
}
});
}
public class Anim extends Thread {
int counter = 0;
int counter2 = 0;
@Override
public void run() {
long last_updated_time = 0;
long delay = 150;
while (true) {
boolean playing = true;
if (playing) {
long current_time = System.currentTimeMillis();
if (current_time > last_updated_time + delay) {
if (counter >= 2) {
counter = 0;
}
if (counter2 >= 10) {
counter2 = 0;
}
draw(purple_bird[counter], red_bird[counter2]);
last_updated_time = current_time;
counter++;
counter2++;
}
}
}
}
private void draw(int purple_bird, int red_bird) {
SurfaceHolder holder = getHolder();
Canvas canvas = holder.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
Bitmap purpleBird = BitmapFactory.decodeResource(getContext().getResources(), purple_bird);
Bitmap redBird = BitmapFactory.decodeResource(getContext().getResources(), red_bird);
Bitmap resizedRedBird = Bitmap.createScaledBitmap(redBird, (int) (redBird.getWidth() * 0.8), (int) (redBird.getHeight() * 0.8), true);
Bitmap resizedPurpleBird = Bitmap.createScaledBitmap(purpleBird, (int) (purpleBird.getWidth() * 0.3), (int) (purpleBird.getHeight() * 0.3), true);
canvas.drawBitmap(resizedRedBird, 100, 100, paint);
canvas.drawBitmap(resizedPurpleBird, 100, 600, paint);
imageButton1.setImageBitmap(resizedPurpleBird);
imageButton2.setImageBitmap(resizedRedBird);
holder.unlockCanvasAndPost(canvas);
}
}
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".playActivity">
<ImageButton
android:id="@+id/imageButton"
android:background="?android:selectableItemBackground"
android:layout_width="151dp"
android:layout_height="229dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.188"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.129"
app:srcCompat="@drawable/bird1" />
<ImageButton
android:id="@+id/imageButton2"
android:background="?android:selectableItemBackground"
android:layout_width="177dp"
android:layout_height="171dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/imageButton"
app:layout_constraintStart_toStartOf="@+id/imageButton"
app:layout_constraintTop_toBottomOf="@+id/imageButton"
app:srcCompat="@drawable/red1" />
</androidx.constraintlayout.widget.ConstraintLayout>