5

I'm trying to make find the difference game app, but I never made anything like that and since I'm a new to development I'm stuck

well baby Steps are always needed to learn xD

I read in some documentation that i have to get each ImageView height and width separately so when i touch imageView1 its coordinates can be set to ImageVIew2 and ViceVersa I MAYBE BE WRONG XD

Right now I have a layout with 2 Images set vertically

<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>

What i want to do is if i tap a place in image1 a circle should be created on the same place in image2

after reading along a few things i have made a circle if i tap the layout but i am stuck after this i cant find what to do next maybe i cant find documentation related to my problems

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;
    }
}}

Right now i think my two ImageView arent separate because when i touch between the 2 images a circle is created which shouldn't since imageView1 ends im not sure if im thinking in right direction its just my guess on how should it work for the game to work I MAYBE BE WRONG XD

See the circle is created even between 2 images

I still have a long way to go, but I'm stuck here
can anyone help?

I replaced my PlayActivity code using Tejas Pandya

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);

    }
}

and i get this error

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)

Md. Zakir Hossain
  • 1,082
  • 11
  • 24
Ahsan raza
  • 57
  • 8

1 Answers1

1

When You tape on Image . You are getting x,y coordinate in

 x = event.getX();
 y = event.getY();

Now you have x,y coordinate . call your drawCircle() method on both of your imageview with this same x,y coordinate.

For Drawing circle :

make one function 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);

}

now for image 1 . use drawCircle(R.id.imageview1,imageview,x,y) and for image 2 . use drawCircle(R.id.imageview2,imageview2,x,y)

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
  • i tried to do that but it i think im doing it wrong since it draws circle on the same place in image1 and doesn't draw circle on image2 – Ahsan raza Jan 03 '19 at 11:56
  • i tried it but it giving in error can you add this in my code and tell me how to use it maybe im doing it wrong – Ahsan raza Jan 05 '19 at 20:09
  • Please look above i edited my question I placed your code inside in PlayActivity i might have placed it wrong. I read in a documentation that you have to get width and height of each image view separately and somehow use it i dont know how. I have read many things in these few days and tried different things but i cant get it to work all i wanted to make was a simple game :'( maybe i picked the wrong game. – Ahsan raza Jan 08 '19 at 06:57
  • @Ahsanraza from your error log it seems like you are getting `null` bitmap in drawCircle method. SO please ensure that bitmap is not null . i've updated my answer – Tejas Pandya Jan 08 '19 at 07:08
  • did i replace the code wrong way ? or is there some other problem – Ahsan raza Jan 08 '19 at 07:23
  • @Ahsanraza you are getting null bitmap .. so see my updated anwer. – Tejas Pandya Jan 08 '19 at 07:29
  • yes i know im getting null object i dont know why since i passing the imageView and its id correctly as you can see in my xml file above and also the PlayActivity when i replaced your code but i dont know why it cant pick it sorry for taking your time im new and i cant find any help from net mostly i find code from github on android studio sample and read and learn but i cant find similar sample on andriod studio – Ahsan raza Jan 08 '19 at 07:55
  • @Ahsanraza you need to do a simple research about getting bitmap from imageview . there are lots of que available . – Tejas Pandya Jan 08 '19 at 08:54