我试图找到与众不同的游戏应用程序,但是我从来没有做过这样的事情,因为我是开发的新手,所以我陷入了困境
宝贝总是需要学习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 arent是分开的,因为当我在两个图像之间触摸时会创建一个圆圈,这不应该是因为imageView1结束时无法确定我是否在正确的方向思考,这只是我对应该如何做的猜测它可以使游戏正常工作,我可能会错XD
我还有很长的路要走,但是我被困在这里
有人可以帮忙吗?
我使用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:尝试在空对象引用上调用虚拟方法'int android.graphics.Bitmap.getWidth()' 在android.graphics.Bitmap.createBitmap(Bitmap.java:742) 在com.example.pc.blazedifferencegame.PlayActivity $ CustomView.drawCircle(PlayActivity.java:68) 在com.example.pc.blazedifferencegame.PlayActivity $ CustomView.onTouchEvent(PlayActivity.java:49)
答案 0 :(得分:0)
当您在Image上录音时。您将在
中获得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)