gpt4 book ai didi

android - 如果我点击 ImageView 1 中的某个位置,则在 ImageView2 上以与 ImageView 1 相同的坐标绘制一个圆圈,反之亦然

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:43 26 4
gpt4 key购买 nike

我正在尝试制作找不同游戏应用程序,但我从来没有做过那样的东西,因为我是开发新手,所以我被困住了

嗯,宝贝,学习总是需要步骤的 xD

我在一些文档中读到我必须分别获取每个 ImageView 的高度和宽度,所以当我触摸 imageView1 时,它的坐标可以设置为 ImageVIew2 和 ViceVersa 我可能错了 XD

现在我有一个垂直设置 2 个图像的布局

<RelativeLayout
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"
android:id="@+id/hello1">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/hello">

<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/pic_9" />

<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/pic_9a" />

</LinearLayout>

我想做的是,如果我点击 image1 中的一个位置,则应在 image2 中的同一位置创建一个圆圈

在阅读了一些内容后,如果我点击布局,我会画一个圆圈,但在此之后我会卡住,我找不到下一步该做什么,也许我找不到与我的问题相关的文档

public class PlayActivity extends AppCompatActivity {

RelativeLayout layout;
float x = 0;
float y = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);

layout=(RelativeLayout) findViewById(R.id.hello1);
layout.addView(new CustomView(PlayActivity.this));

}

public class CustomView extends View {

Bitmap mBitmap;
Paint paint;

public CustomView(Context context) {
super(context);
mBitmap = Bitmap.createBitmap(400, 800, Bitmap.Config.ARGB_8888);
paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, 50, paint);
}

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = event.getX();
y = event.getY();
invalidate();
}
return false;
}
}}

现在我认为我的两个 ImageView 没有分开,因为当我在 2 个图像之间触摸时会创建一个圆圈,这不应该因为 imageView1 结束我不确定我是否在正确的方向思考它只是我的猜测应该如何它可以让游戏正常运行我可能错了 XD

See the circle is created even between 2 images

我还有很长的路要走,但我被困在这里
谁能帮忙?

我用 Tejas Pandya 替换了我的 PlayActivity 代码

RelativeLayout layout;
float x = 0;
float y = 0;
ImageView ImageView1, ImageView2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);

layout=(RelativeLayout) findViewById(R.id.hello1);
layout.addView(new CustomView(PlayActivity.this));

ImageView1 = (ImageView) findViewById(R.id.image1);
ImageView2 = (ImageView) findViewById(R.id.image2);

}

public class CustomView extends View {


public CustomView(Context context) {
super(context);
}

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
x = event.getX();
y = event.getY();
invalidate();
drawCircle(R.id.image2,ImageView2,x,y);
}
return false;
}

public void drawCircle(int my_image_id, ImageView my_imageview, float x , float y){

BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), my_image_id,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);


Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);


Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(x, y, 25, paint);


my_imageview.setAdjustViewBounds(true);
my_imageview.setImageBitmap(mutableBitmap);

}
}

我得到了这个错误

E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at android.graphics.Bitmap.createBitmap(Bitmap.java:742) at com.example.pc.blazedifferencegame.PlayActivity$CustomView.drawCircle(PlayActivity.java:68) at com.example.pc.blazedifferencegame.PlayActivity$CustomView.onTouchEvent(PlayActivity.java:49)

最佳答案

当您在图像上粘贴时。你在

中得到 x,y 坐标
 x = event.getX();
y = event.getY();

现在您有了 x,y 坐标。使用相同的 x,y 坐标在两个 ImageView 上调用您的 drawCircle() 方法。

绘制圆圈:

制作一个函数drawCircle();

public void drawCircle(int my_image_id,Imageview my_imageview,int x ,int y){

BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), my_image_id,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);


//please check here . you've got your bitmap or it is null.
// if it is null,there might be some problem with decoding your resources above.
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);


Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(x, y, 25, paint);


my_imageview.setAdjustViewBounds(true);
my_imageview.setImageBitmap(mutableBitmap);

}

现在为 图片 1。使用 drawCircle(R.id.imageview1,imageview,x,y) 和 图片 2。使用 drawCircle(R.id.imageview2,imageview2,x,y)

关于android - 如果我点击 ImageView 1 中的某个位置,则在 ImageView2 上以与 ImageView 1 相同的坐标绘制一个圆圈,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com