2013-02-05 10 views
11

में 2 छवियों को जोड़ना मैं एंड्रॉइड में 2 छवियों को जोड़ना चाहता हूं। उदाहरण के लिए,एंड्रॉइड

enter image description here

enter image description here

मैं उन्हें इस तरह होना चाहते हैं, enter image description here

मैं पहले से ही यह मुश्किल कोडिंग के द्वारा किया जाता है। सबसे पहले मुझे हरी छवि के ऊपरी बाएं मिलते हैं और फिर हरे रंग की तरफ का सबसे निचला बायां बिंदु मिलता है। मैं इसे टच इवेंट की घटना के साथ करता हूं .gerRawX() और event.getRawY() पैरामीटर। अब मैं उन दो बिंदुओं के बीच एक्स और वाई में दूरी जानता हूं। मैं लाल रंग के लिए भी वही काम करता हूं। अब जब लाल टुकड़े लाल के करीब चले जाते हैं तो मैं गणना करता हूं कि लाल टुकड़े के ऊपरी बाएं बिंदु हरे रंग के टुकड़े के निचले बाएं के पास है या नहीं। यदि ऐसा है तो मैं हरा एक/लाल एक दूसरे को अनुवाद करता हूं। लेकिन यह हार्ड कोडित गणना एक ही आकार के टैबलेट या विभिन्न संकल्प के साथ फोन के लिए विफल होना चाहिए। मैं सिर्फ यह जानना चाहता हूं कि मैं समाधान को कैसे सामान्यीकृत करूं। धन्यवाद।

संपादित करें: मेरी गेम एक्टिविटी और इमेजइन्फो क्लास जहां मैं दोनों छवियों को जोड़ने का प्रयास करता हूं। मेरे पास वास्तव में इस तरह की 6 छवियां हैं I और मैं उन्हें कनेक्ट करना चाहता हूँ। छवि 1 की तरह छवि 2 और छवि 2 से 3 तक कनेक्ट होगा और इसी तरह।

GameActivity वर्ग

package com.example.JigSawPuzzle; 


import com.example.test.R; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.Context;  
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.graphics.Point; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.Display; 
import android.view.MotionEvent; 
import android.view.Surface; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup.LayoutParams; 

import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

@SuppressLint("NewApi") 
public class GameActivity extends Activity implements OnTouchListener 
{ 
static double screenInches; 
int touchStartX = 0; 
int touchStartY = 0; 

int diffX = 0; 
int diffY = 0; 
int attachedPieces=0; 
int imageX = 0; 
int imageY = 0; 
int height,width; 
boolean [] flag = new boolean[7]; 
boolean isPortait; 
RelativeLayout relataivelayoutLayout; 
RelativeLayout.LayoutParams paramsA,paramsB,paramsC,paramsD,paramsE,paramsF; 
ImageView imageA,imageB,imageC,imageD,imageE,imageF; 
ImageInfo [] imageInfoArray = new ImageInfo[7]; 

// added for sound effect 
private SoundPool soundPool; 
private int correctPieceAttachSoundId,gameFinishSoundId; 
private boolean loaded = false; 

//for 10inch landscape height = 752, width = 1280; portrait height = 1232 width = 800 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    isPortait = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ? false : true; 
    Display display = getWindowManager().getDefaultDisplay(); 
    height = display.getHeight(); 
    width = display.getWidth();  

    imageA = new ImageView(this); 
    imageB = new ImageView(this); 
    imageC = new ImageView(this); 
    imageD = new ImageView(this); 
    imageE = new ImageView(this); 
    imageF = new ImageView(this); 

    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    double x = Math.pow(dm.widthPixels/dm.xdpi,2); 
    double y = Math.pow(dm.heightPixels/dm.ydpi,2); 
    screenInches = Math.sqrt(x+y); 


    if(screenInches>9.0) 
    { 

     imageA.setBackgroundResource(R.drawable.a); 
     imageB.setBackgroundResource(R.drawable.b); 
     imageC.setBackgroundResource(R.drawable.c); 
     imageD.setBackgroundResource(R.drawable.d); 
     imageE.setBackgroundResource(R.drawable.e); 
     imageF.setBackgroundResource(R.drawable.f); 
    } 
    else 
    { 
     imageA.setBackgroundResource(R.drawable.aa);  
     imageB.setBackgroundResource(R.drawable.bb); 
     imageC.setBackgroundResource(R.drawable.cc); 
     imageD.setBackgroundResource(R.drawable.dd); 
     imageE.setBackgroundResource(R.drawable.ee); 
     imageF.setBackgroundResource(R.drawable.ff); 
    } 



    imageA.setId(1); 
    imageA.setTag("a"); 
    paramsA = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option)     
    imageA.setLayoutParams(paramsA); 
    imageA.setOnTouchListener(this); 
    //Log.d("GameActivity", "") 



    imageB.setId(2); 
    imageB.setTag("b"); 
    paramsB = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option) 
    imageB.setLayoutParams(paramsB); 
    imageB.setOnTouchListener(this); 



    imageC.setId(3); 
    imageC.setTag("c"); 
    paramsC = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option) 
    imageC.setLayoutParams(paramsC); 
    imageC.setOnTouchListener(this); 


    imageD.setId(4); 
    imageD.setTag("d"); 
    paramsD = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option) 
    imageD.setLayoutParams(paramsD); 
    imageD.setOnTouchListener(this); 


    imageE.setId(5); 
    imageE.setTag("e"); 
    paramsE = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option) 
    imageE.setLayoutParams(paramsE); 
    imageE.setOnTouchListener(this); 

    imageF.setId(6); 
    imageF.setTag("f"); 
    paramsF = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //The WRAP_CONTENT parameters can be replaced by an absolute width and height or the FILL_PARENT option) 
    imageF.setLayoutParams(paramsF); 
    imageF.setOnTouchListener(this); 
    setupPieces(); 


    // Set the hardware buttons to control the music 
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC); 
    // Load the sound 
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() 
    { 
     @Override 
     public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 

      loaded = true; 
     } 
    }); 

    gameFinishSoundId = soundPool.load(this, R.raw.bells, 1); 
    correctPieceAttachSoundId= soundPool.load(this, R.raw.bell, 1); 

} 

public void playSound(int soundId) 
{ 
    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    float actualVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
    float maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 
    float volume = actualVolume/maxVolume; 
    // Is the sound loaded already? 
    if (loaded) 
    { 
     soundPool.play(soundId, volume, volume, 1, 0, 1.0f); 
    } 
} 

public void onConfigurationChanged(Configuration newConfig) 
{ 
    super.onConfigurationChanged(newConfig); 

    // Checks the orientation of the screen 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
//  Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
    } 
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) 
    { 
//  Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
    } 
// Toast.makeText(this, "in OnConfiguaration Changed", Toast.LENGTH_SHORT).show(); 
    isPortait = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) ? false : true; 
    ((RelativeLayout)imageA.getParent()).removeAllViews(); 
    Log.d("GameActivity", "in onconfigurationChanged"); 
    setupPieces(); 
} 
public void setupPieces() 
{ 
    RelativeLayout relataivelayoutLayout = new RelativeLayout(this); 
    Display display = getWindowManager().getDefaultDisplay(); 
    height = display.getHeight(); 
    width = display.getWidth(); 



    if(!isPortait) 
    { 
     paramsA.leftMargin = 10; //Your X coordinate 
     paramsA.topMargin = 30; //Your Y coordinate 
    } 
    else 
    { 
     paramsA.leftMargin = 30; //Your X coordinate 
     paramsA.topMargin = 10; //Your Y coordinate 
    } 


    imageA.setLayoutParams(paramsA); 
    imageA.setOnTouchListener(this); 
    //Log.d("GameActivity", "") 


    if(!isPortait) 
    { 
     paramsB.leftMargin = 650; //Your X coordinate 
     paramsB.topMargin = 300; //Your Y coordinate 
    } 
    else 
    { 
     paramsB.leftMargin = 300; //Your X coordinate 
     paramsB.topMargin = 750; //Your Y coordinate 
    } 
    imageB.setLayoutParams(paramsB); 
    imageB.setOnTouchListener(this); 


    if(!isPortait) 
    { 
     paramsC.leftMargin = 400; //Your X coordinate 
     paramsC.topMargin = 380; //Your Y coordinate 
    } 
    else 
    { 
     paramsC.leftMargin = 400; //Your X coordinate 
     paramsC.topMargin = 350; //Your Y coordinate 
    } 
    imageC.setLayoutParams(paramsC); 
    imageC.setOnTouchListener(this); 


    if(!isPortait) 
    { 
     paramsD.leftMargin = 750; //Your X coordinate 
     paramsD.topMargin = 20; //Your Y coordinate 
    } 
    else 
    { 
     paramsD.leftMargin = 20; //Your X coordinate 
     paramsD.topMargin = 750; //Your Y coordinate 
    } 
    imageD.setLayoutParams(paramsD); 
    imageD.setOnTouchListener(this); 


    if(!isPortait) 
    { 
     paramsE.leftMargin = 900; //Your X coordinate 
     paramsE.topMargin = 400; //Your Y coordinate 
    } 
    else 
    { 
     paramsE.leftMargin = 475; //Your X coordinate 
     paramsE.topMargin = 700; //Your Y coordinate 
    } 
    imageE.setLayoutParams(paramsE); 
    imageE.setOnTouchListener(this); 


    if(!isPortait) 
    { 
     paramsF.leftMargin = 90; //Your X coordinate 
     paramsF.topMargin = 300; //Your Y coordinate 
    } 
    else 
    { 
     paramsF.leftMargin = 90; //Your X coordinate 
     paramsF.topMargin = 300; //Your Y coordinate 
    } 
    imageF.setLayoutParams(paramsF); 
    imageF.setOnTouchListener(this); 
    ImageInfo imageAinfo = new ImageInfo(imageA.getId(),imageA,1,2); 
    imageInfoArray[0] = imageAinfo; 
    ImageInfo imageBinfo = new ImageInfo(imageB.getId(),imageB,1,3); 
    imageInfoArray[1] = imageBinfo; 
    ImageInfo imageCinfo = new ImageInfo(imageC.getId(),imageC,2,4); 
    imageInfoArray[2] = imageCinfo; 
    ImageInfo imageDinfo = new ImageInfo(imageD.getId(),imageD,3,5); 
    imageInfoArray[3] = imageDinfo; 
    ImageInfo imageEinfo = new ImageInfo(imageE.getId(),imageE,4,6); 
    imageInfoArray[4] = imageEinfo; 
    ImageInfo imageFinfo = new ImageInfo(imageF.getId(),imageF,5,6); 
    imageInfoArray[5] = imageFinfo; 



    relataivelayoutLayout.addView(imageA); 
    relataivelayoutLayout.addView(imageB); 
    relataivelayoutLayout.addView(imageC); 
    relataivelayoutLayout.addView(imageD); 
    relataivelayoutLayout.addView(imageE); 
    relataivelayoutLayout.addView(imageF); 


    setContentView(relataivelayoutLayout); 


} 
private void updatePosition(int id) 
{ 
    if(flag[imageInfoArray[id-1].id]) 
     return; 
    flag[imageInfoArray[id-1].id] = true; 

    RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams)imageInfoArray[id-1].imageView.getLayoutParams(); 
    param.leftMargin = imageInfoArray[id-1].imageView.getLeft() + diffX; 
    param.topMargin = imageInfoArray[id-1].imageView.getTop() + diffY; 
    imageInfoArray[id-1].imageView.setLayoutParams(param); 
    if(imageInfoArray[id-1].isTopConnected) 
     updatePosition(imageInfoArray[id-1].topPieceId); 
    if(imageInfoArray[id-1].isBottomConnected) 
     updatePosition(imageInfoArray[id-1].bottomPieceId); 
    return; 
} 
@Override 
public boolean onTouch(View v, MotionEvent event) 
{ 

    if(v instanceof ImageView) 
    { 

     ImageView imageView = (ImageView) v; 
     ImageInfo imageInfo = imageInfoArray[imageView.getId()-1]; 

     switch (event.getAction() & MotionEvent.ACTION_MASK) 
     { 

      case MotionEvent.ACTION_DOWN: 

       touchStartX = (int) event.getRawX(); 
       touchStartY = (int) event.getRawY(); 
       imageX = imageView.getLeft(); 
       imageY = imageView.getTop(); 
       //Toast.makeText(this, "x = "+event.getRawX()+" y = "+event.getRawY(), Toast.LENGTH_SHORT).show(); 
       break; 

      case MotionEvent.ACTION_UP: 

       touchStartX = (int) event.getRawX(); 
       touchStartY = (int) event.getRawY(); 
       imageX = imageView.getLeft(); 
       imageY = imageView.getTop(); 
       int id = imageInfo.id; 
       while(imageInfo.isTopConnected) 
       { 
        if(imageInfo.id == imageInfo.topPieceId) 
         break; 
        imageInfo = imageInfoArray[imageInfo.topPieceId-1];      
       } 
       if(!imageInfo.isTopConnected) 
       { 
        imageView = imageInfo.imageView; 
        int topConnectingPieceId = imageInfo.topPieceId; 
        int topConnectingX=0,topConnectingY=0; 
        topConnectingX = imageInfo.calculateTopX(imageView.getLeft(), imageView.getId()); 
        topConnectingY = imageInfo.calculateTopY(imageView.getTop(), imageView.getId()); 


        ImageInfo topImageInfo = imageInfoArray[topConnectingPieceId-1]; 
        int bottomConnectingX = topImageInfo.calculateBottomX(topImageInfo.imageView.getLeft(),topConnectingPieceId); 
        int bottomConnectingY = topImageInfo.calculateBottomY(topImageInfo.imageView.getTop(),topConnectingPieceId); 
        diffX = (bottomConnectingX-topConnectingX); 
        diffY = (bottomConnectingY-topConnectingY); 
        if(Math.abs(diffX)<=20 && Math.abs(diffY)<=20) 
        { 
         for(int i=0;i<7;i++) 
          flag[i]=false; 
         updatePosition(imageInfo.id); 
         imageInfo.setIsTopConnected(true); 
         topImageInfo.setIsBottomConnected(true); 
         attachedPieces++; 
         if(attachedPieces==5) 
         { 
          setupFinishDialogue(); 
          playSound(gameFinishSoundId); 
         } 
         else 
          playSound(correctPieceAttachSoundId); 
         break; 
        } 
       } 
       imageInfo = imageInfoArray[id-1]; 
       while(imageInfo.isBottomConnected) 
       { 
        if(imageInfo.id == imageInfoArray[imageInfo.bottomPieceId-1].id) 
         break; 
        imageInfo = imageInfoArray[imageInfo.bottomPieceId-1];      
       } 
       imageView = imageInfo.imageView; 
       if(!imageInfo.isBottomConnected) 
       { 
        int topConnectingX=0,topConnectingY=0; 
        int bottomConnectingX = imageInfo.calculateBottomX(imageView.getLeft(), imageView.getId()); 
        int bottomConnectingY = imageInfo.calculateBottomY(imageView.getTop(), imageView.getId()); 
        int bottomConnectingPieceId = imageInfo.bottomPieceId; 
        ImageInfo bottomImageInfo = imageInfoArray[bottomConnectingPieceId-1]; 
        topConnectingX = bottomImageInfo.calculateTopX(bottomImageInfo.imageView.getLeft(),bottomConnectingPieceId); 
        topConnectingY = bottomImageInfo.calculateTopY(bottomImageInfo.imageView.getTop(), bottomConnectingPieceId); 
        diffX = (topConnectingX-bottomConnectingX); 
        diffY = (topConnectingY-bottomConnectingY); 
        if(Math.abs(diffX)<=20 && Math.abs(diffY)<=20) 
        { 
         for(int i=0;i<7;i++) 
          flag[i]=false; 
         updatePosition(imageInfo.id); 
         imageInfo.setIsBottomConnected(true); 
         bottomImageInfo.setIsTopConnected(true); 
         attachedPieces++; 
         if(attachedPieces==5) 
         { 
          setupFinishDialogue(); 
          playSound(gameFinishSoundId); 
         } 
         else 
          playSound(correctPieceAttachSoundId); 
        } 
       } 

       break; 

      case MotionEvent.ACTION_MOVE: 

       diffX = (int) (event.getRawX() - touchStartX); 
       diffY = (int) (event.getRawY() - touchStartY); 
       touchStartX = (int)event.getRawX(); 
       touchStartY = (int)event.getRawY(); 

       for(int i=0;i<7;i++) 
        flag[i]=false;     
       updatePosition(imageInfo.id);  
       break; 

      default: 
       break; 
     } 

    } 

    return true; 
} 

void lockOrientation() 
{ 
    switch (getResources().getConfiguration().orientation) 
    { 
     case Configuration.ORIENTATION_PORTRAIT: 
      if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) 
      { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
      } 
      else 
      { 
       int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
       if(rotation == android.view.Surface.ROTATION_90|| rotation == android.view.Surface.ROTATION_180) 
       { 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
       } 
       else 
       { 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
       } 
      } 
      break; 

     case Configuration.ORIENTATION_LANDSCAPE: 
      if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) 
      { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
      } 
      else 
      { 
       int rotation = getWindowManager().getDefaultDisplay().getRotation(); 
       if(rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90){ 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
       } 
       else 
       { 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
       } 
      } 
      break; 
    } 
} 

void setupFinishDialogue() 
{ 
    /*if(getWindowManager().getDefaultDisplay().getRotation()==3) 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    else if(getWindowManager().getDefaultDisplay().getRotation()==1) 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
    else if(getWindowManager().getDefaultDisplay().getRotation()==2) 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
    else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);*/ 


    lockOrientation(); 

    AlertDialog.Builder builderForAlertBox = new AlertDialog.Builder(this); 
    builderForAlertBox.setCancelable(false).setMessage("Good Job!").setPositiveButton("Restart", dialogClickListner).setNegativeButton("Quit", dialogClickListner). 
    setCancelable(true).show(); 
} 
DialogInterface.OnClickListener dialogClickListner = new DialogInterface.OnClickListener() 
{  
    public void onClick(DialogInterface dialog, int which) 
    { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 
     switch (which) { 
     case DialogInterface.BUTTON_POSITIVE: 
      finish(); 
      Intent intent = new Intent(getApplicationContext(),GameActivity.class); 
      startActivity(intent); 
      break; 

     case DialogInterface.BUTTON_NEGATIVE: 
      finish(); 
     default: 
      break; 
     } 

    } 
}; 

}

ImageInfo कक्षा

package com.example.JigSawPuzzle; 

import android.widget.ImageView; 
import android.widget.Toast; 

public class ImageInfo 
{ 
ImageView imageView; 
int imageATopLeftDifferenceX = -1; 
int imageATopLeftDifferenceY = -1; 
int imageABottomRightDifferenceX = 113; 
int imageABottomRightDifferenceY = 140; 

int imageBTopLeftDifferenceX = 0; 
int imageBTopLeftDifferenceY = 0; 
int imageBBottomRightDifferenceX = 0; 
int imageBBottomRightDifferenceY = 111; 

int imageCTopLeftDifferenceX = 14; 
int imageCTopLeftDifferenceY = 0; 
int imageCBottomRightDifferenceX = 0; 
int imageCBottomRightDifferenceY = 88; 

int imageDTopLeftDifferenceX = 92; 
int imageDTopLeftDifferenceY = 2; 
int imageDBottomRightDifferenceX = 0; 
int imageDBottomRightDifferenceY = 70; 

/*int imageETopLeftDifferenceX = 0; 
int imageETopLeftDifferenceY = 0; 
int imageEBottomRightDifferenceX = 55; 
int imageEBottomRightDifferenceY = 112;*/ 
int imageETopLeftDifferenceX = 55; 
int imageETopLeftDifferenceY = 112; 
int imageEBottomRightDifferenceX = 0; 
int imageEBottomRightDifferenceY = 0; 

/*int imageFTopLeftDifferenceX = 0; 
int imageFTopLeftDifferenceY = 26; 
int imageFBottomRightDifferenceX = 0; 
int imageFBottomRightDifferenceY = 109; 
int id,topPieceId,bottomPieceId;*/ 
int imageFTopLeftDifferenceX = 0; 
int imageFTopLeftDifferenceY = 109; 
int imageFBottomRightDifferenceX = 0; 
int imageFBottomRightDifferenceY = 26; 
int id,topPieceId,bottomPieceId; 

boolean isTopConnected = false; 
boolean isBottomConnected = false; 
public ImageInfo(int id,ImageView imageView,int topPieceId,int bottomPieceId) 
{ 
    this.topPieceId = topPieceId; 
    this.bottomPieceId = bottomPieceId; 
    this.imageView = imageView; 
    this.id = id; 
    if(id==1) 
     isTopConnected = true; 
    else if(id==6) 
     isBottomConnected = true; 
    if(GameActivity.screenInches>9.0) 
     initializePiecesInfo(); 
} 
private void initializePiecesInfo() 
{ 

    imageATopLeftDifferenceX = 0; 
    imageATopLeftDifferenceY = 0; 
    imageABottomRightDifferenceX = 150; 
    imageABottomRightDifferenceY = 184; 

    imageBTopLeftDifferenceX = 0; 
    imageBTopLeftDifferenceY = 0; 
    imageBBottomRightDifferenceX = 0; 
    imageBBottomRightDifferenceY = 148; 

    imageCTopLeftDifferenceX = 23; 
    imageCTopLeftDifferenceY = 0; 
    imageCBottomRightDifferenceX = 0; 
    imageCBottomRightDifferenceY = 115; 

    imageDTopLeftDifferenceX = 121; 
    imageDTopLeftDifferenceY = 0; 
    imageDBottomRightDifferenceX = 0; 
    imageDBottomRightDifferenceY = 91; 

    /*int imageETopLeftDifferenceX = 0; 
    int imageETopLeftDifferenceY = 0; 
    int imageEBottomRightDifferenceX = 55; 
    int imageEBottomRightDifferenceY = 112;*/ 
    imageETopLeftDifferenceX = 74; 
    imageETopLeftDifferenceY = 147; 
    imageEBottomRightDifferenceX = 0; 
    imageEBottomRightDifferenceY = 0; 

    /*int imageFTopLeftDifferenceX = 0; 
    int imageFTopLeftDifferenceY = 26; 
    int imageFBottomRightDifferenceX = 0; 
    int imageFBottomRightDifferenceY = 109; 
    int id,topPieceId,bottomPieceId;*/ 
    imageFTopLeftDifferenceX = 0; 
    imageFTopLeftDifferenceY = 144; 
    imageFBottomRightDifferenceX = 0; 
    imageFBottomRightDifferenceY = 26; 
} 

int calculateTopX(int realX,int id) 
{ 
    if(id==2) 
     return realX+imageBTopLeftDifferenceX; 
    if(id==3) 
     return realX+imageCTopLeftDifferenceX; 
    if(id==4) 
     return realX+imageDTopLeftDifferenceX; 
    if(id==5) 
     return realX+imageETopLeftDifferenceX; 
    if(id==6) 
     return realX+imageFTopLeftDifferenceX; 
    return realX; 

} 
int calculateTopY(int realY,int id) 
{ 
    if(id==2) 
     return realY+imageBTopLeftDifferenceY; 
    if(id==3) 
     return realY+imageCTopLeftDifferenceY; 
    if(id==4) 
     return realY+imageDTopLeftDifferenceY; 
    if(id==5) 
     return realY+imageETopLeftDifferenceY; 
    if(id==6) 
     return realY+imageFTopLeftDifferenceY; 
    return realY; 
} 

int calculateBottomX(int realX,int id) 
{ 
    if(id==1) 
     return realX+imageABottomRightDifferenceX; 
    if(id==2) 
     return realX+imageBBottomRightDifferenceX; 
    if(id==3) 
     return realX+imageCBottomRightDifferenceX; 
    if(id==4) 
     return realX+imageDBottomRightDifferenceX; 
    if(id==5) 
     return realX+imageEBottomRightDifferenceX; 
    return realX+imageFBottomRightDifferenceX;  
} 
int calculateBottomY(int realY,int id) 
{ 
    if(id==1) 
     return realY+imageABottomRightDifferenceY; 
    if(id==2) 
     return realY+imageBBottomRightDifferenceY; 
    if(id==3) 
     return realY+imageCBottomRightDifferenceY; 
    if(id==4) 
     return realY+imageDBottomRightDifferenceY; 
    if(id==5) 
     return realY+imageEBottomRightDifferenceY; 
    return realY+imageFBottomRightDifferenceY; 
} 
void setIsTopConnected(boolean isTopConnected) 
{ 
    this.isTopConnected = isTopConnected; 
} 
void setIsBottomConnected(boolean isBottomConnected) 
{ 
    this.isBottomConnected = isBottomConnected; 
} 

} 
+0

मुझे लगता है कि आप इसे सही लागू नहीं कर सकते .... छवियों को आप लाल और हरे रंग – Pragnani

+0

के बीच की रेखा से छूट सकती भी पार कर जाते लेकिन कुछ समाधान वहीं होना चाहिए? अब मैं केवल यह जांचता हूं कि यह 7 इंच टैब या 10 इंच टैब है या नहीं। क्या मुझे यह भी जांचना चाहिए कि 7/10 इंच टैब और विभिन्न संकल्प भी हैं? – Tahlil

+1

क्या आप सटीक दो छवियां अपलोड कर सकते हैं? यदि आकार के साथ एक अलग छवि में प्रत्येक आकार बस छवि फिट बैठता है। उन्हें जोड़ना आसान होगा। यदि दोनों एक ही छवि में आकार देते हैं, तो आपको अधिक काम करने की आवश्यकता होगी। साथ ही, उस कोड को अपलोड करें जिसे आप वर्तमान में – iTech

उत्तर

18

छवियों आप इसे उन्हें एक सामान्य तरीके से कनेक्ट करने के लिए हैं क्योंकि सभी चित्रों में एक ही चौड़ाई सीधा है प्रदान की यह देखते हुए और उनके पास पारदर्शी पृष्ठभूमि है, यहां विचार है:

1- आपको दो चित्र है, जो या तो आप नीचे या ऊपर छवि से गणना कर सकते हैं के बीच ओवरलैप दूरी की जरूरत

enter image description here

नीचे चित्र में हरे रंग की रेखा शीर्ष में लाल रेखा के बराबर होना चाहिए चित्र

bottom image

को देखते हुए अपनी छवियों को पारदर्शी पृष्ठभूमि आप आसानी से इस दूरी की गणना कर सकते हैं कि, मैं यहाँ नीचे छवि का उपयोग करेगा।

विचार नीचे बिटमैप (यानी चौड़ाई -1) के अंतिम कॉलम में प्रत्येक पिक्सेल को जांचना है और एक बार जब आप एक गैर पारदर्शी पिक्सेल हिट करते हैं तो रोकें।

private int getOverlapDistance(Bitmap bottomBitmap) { 
    int height = bottomBitmap.getHeight(); 
    int width = bottomBitmap.getWidth(); 
    int distance = 0; 
    for (int i = 0; i < height; i++) { 
     if (Color.alpha(bottomBitmap.getPixel(width - 1, i)) != 0) { 
      distance = (i + 1); 
      break; 
     } 
    } 
    return distance; 
} 

उन्हें कनेक्ट करने के लिए आप की तरह (यह मानते हुए आप ऊपर और नीचे की छवि के लिए अलग ImageView) कुछ कर सकते हैं:

bitmap = ((BitmapDrawable) bottomImage.getDrawable()).getBitmap(); 
int overlapDistance = getOverlapDistance(bitmap); 
bottomImage.setTop(topImage.getBottom() - overlapDistance); 

असल में मैं एक सरल गतिविधि के साथ इस कोशिश की और यह काम कर रहा है, यहाँ

Images before connecting

मैं सिर्फ ऊपर कॉड निष्पादित करें: यह कैसे पहले की तरह और दो छवियों से जोड़ने के बाद लग रही है ई जब बटन क्लिक किया जाता है connect

After connecting

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^