首页 > 解决方案 > 将 ImageButtons 附加到 OnClickListener 的位图

问题描述

我在 SurfaceView 上创建了两个位图动画。我希望这些位图是可点击的。我通过一些研究了解到您不能将 OnClickListener 添加到位图中。因此,我尝试在我的 XML 文件中添加两个 ImageButton,并计划使用 .setImageBitmap 将它们附加到我的位图

动画效果很好,但是自从尝试添加充气机来访问我的 XML 和 OnClickListener 之后,一切都发生了变化。

我刚刚开始编程,这只是我一直在做的一个小项目。我不确定 Imagebutton 方法是否错误。几天来,我一直在尝试让我的动画可以点击。

任何帮助是极大的赞赏

日志猫

error: incompatible types: playActivity.surfaceView cannot be converted to Context

代码

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>

标签: javaandroidbitmapsurfaceview

解决方案


推荐阅读