2012-01-03 11 views
22

मैं एंड्रॉयड में एक विशिष्ट कोण के अनुसार छवि को घुमाने के लिए चाहते हैं एक कैनवास पर छवि घूर्णन, एक कम्पास की तरह कुछ बात ...में एंड्रॉयड

मैं इस कोड को ... यह drawPath() पर काम करता है, लेकिन मैं पथ और चित्रकारी छवि को छवि के साथ प्रतिस्थापित करना चाहता हूं .. मैंने बिटमैप छवि बनाने की कोशिश की, DrawBitmapImage, लेकिन छवि पथ की तरह घूमती नहीं है .. कोई मदद PLease?

public void draw(Canvas canvas) { 
    double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude); 

    //Correction; 
    angle-=90; 

    //Correction for azimuth 
    angle-=azimuth; 

    if((getContext() instanceof Activity) && ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90; 

    while(angle<0)angle=angle+360; 

    Rect rect = canvas.getClipBounds(); 

    int height = rect.bottom-rect.top; 
    int width = rect.right-rect.left; 
    int left = rect.left; 
    int top = rect.top; 

    if(height>width){ 
     top+=(height-width)/2; 
     height=width; 
    } 
    if(width>height){ 
     left+=(width-height)/2; 
     width=height; 
    } 

    float centerwidth = width/2f; 
    float centerheight = height/2f; 

    Paint p = new Paint(); 
    p.setColor(color); 
    p.setStyle(Paint.Style.FILL); 
    p.setAntiAlias(true); 

    float startX = left+(float)(centerwidth+Math.cos(deg2rad(angle))*width/3.0); 
    float startY = top+(float)(centerheight+Math.sin(deg2rad(angle))*height/3.0); 

    Path path = new Path(); 
    path.moveTo(
      startX, 
      startY); 
    path.lineTo(
      left+(float)(centerwidth+Math.cos(deg2rad(angle+140))*width/4.0), 
      top+(float)(centerheight+Math.sin(deg2rad(angle+140))*height/4.0)); 
    path.lineTo(
      left+(float)centerwidth, 
      top+(float)centerheight 
      ); 
    path.lineTo(
      left+(float)(centerwidth+Math.cos(deg2rad(angle+220))*width/4.0), 
      top+(float)(centerheight+Math.sin(deg2rad(angle+220))*height/4.0) 
      ); 

    path.lineTo(
      startX, 
      startY 
      ); 




    canvas.drawPath(path, p); 
} 

उत्तर

48

आप या तो एक मैट्रिक्स का उपयोग करके अपने बिटमैप बारी बारी से जब आप इसे आकर्षित:

Matrix matrix = new Matrix(); 
matrix.setRotate(angle, imageCenterX, imageCenterY); 
yourCanvas.drawBitmap(yourBitmap, matrix, null); 

आप ड्राइंग से पहले कैनवास घूर्णन करके भी कर सकते हैं:

yourCanvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated. 
yourCanvas.rotate(-angle); 
yourCanvas.drawBitmap(yourBitmap, left, top, null); 
yourCanvas.restore(); 

वह विकल्प चुनें जो आपको सबसे अच्छा लगा।

+0

@ जेव, मुझे लगता है कि पहला दृष्टिकोण इस्तेमाल किया जा सकता है, विशेष रूप से, जहां आप x-axis और/या y-axis के आस-पास एक छवि या बिटमैप को घुमाने के लिए 'कैनवास' के रूप में घूर्णन विधियां नहीं रखते हैं, यह एक्सवाई-प्लेन में करता है। मुझे लगता है कि कोई 'कैमरा' ऑब्जेक्ट प्राप्त कर सकता है और आवश्यक 3 डी ट्रांसफॉर्मेशन कर सकता है और आखिरकार 'मैट्रिक्स' प्रस्तुति को प्राप्त करने के लिए 'getMatrix' पासिंग 'मैट्रिक्स' उदाहरण का उपयोग कर सकता है। अंत में, इस 'मैट्रिक्स' को 'drawBitmap' विधि में पास करें। विशेष रूप से, [यहां] (http://stackoverflow.com/questions/32554925/android-animate-rotation-of-map-marker-around-x-and-y-axis): कोई भी विचार। धन्यवाद! –

3

आपको पहले कैनवास को घुमाएं और फिर जो कुछ भी आप चाहते हैं उसे खींचें। फिर खींची गई वस्तु स्क्रीन पर घूमने के रूप में दिखाई देगी।

canvas.rotate(45); // degrees to rotate 

इसे अपने अच्छे तरीके से आजमाएं।

चेक इस ट्यूटोरियल आप कैसे बिटमैप आकर्षित करने के लिए और कैनवास बारी बारी से करने के बारे में जानकारी मिल जाएगी

Check complete tutorial

+0

कोण के अनुसार कैनवास घुमाएं? फिर क्या? बस बिटमैप बनाना और इसे – Reham

+0

खींचें ऐसा लगता है कि आपने एंड्रॉइड डेवलपर्स वेबसाइट पर उपलब्ध जानकारी नहीं पढ़ी है। कैनवास कक्षा में बिटमैप को आकर्षित करने के लिए कई फ़ंक्शन उपलब्ध हैं। आपको अपनी आवश्यकता के अनुसार canvas.rotate() के बाद किसी भी drwaBitmap() को कॉल करना होगा। –

+0

@Reham नया उत्तर देखें –

1

@Reham: नीचे दिए गए इस उदाहरण कोड को देखो

public class bitmaptest extends Activity { 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    LinearLayout linLayout = new LinearLayout(this); 

    // load the origial BitMap (500 x 500 px) 
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
      R.drawable.android); 

    int width = bitmapOrg.width(); 
    int height = bitmapOrg.height(); 
    int newWidth = 200; 
    int newHeight = 200; 

    // calculate the scale - in this case = 0.4f 
    float scaleWidth = ((float) newWidth)/width; 
    float scaleHeight = ((float) newHeight)/height; 

    // createa matrix for the manipulation 
    Matrix matrix = new Matrix(); 
    // resize the bit map 
    matrix.postScale(scaleWidth, scaleHeight); 
    // rotate the Bitmap 
    matrix.postRotate(45); 

    // recreate the new Bitmap 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
         width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever 
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

    ImageView imageView = new ImageView(this); 

    // set the Drawable on the ImageView 
    imageView.setImageDrawable(bmd); 

    // center the Image 
    imageView.setScaleType(ScaleType.CENTER); 

    // add ImageView to the Layout 
    linLayout.addView(imageView, 
      new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT 
      ) 
    ); 

    // set LinearLayout as ContentView 
    setContentView(linLayout); 
} 
} 

आप मैट्रिक्स का उपयोग करने के बारी बारी से करने के लिए छवि लाइनों

matrix.postRotate(45); - 

इस 45 डिग्री

करने के लिए छवि को घुमा देगी देखो है

आशा है कि यह आपकी मदद करेगा ... thx

+0

मुझे लगता है कि आपने 'bitmapOrg.width()' 'bitmapOrg.getWidth()' के लिए टाइप किया है। –

1

निम्नलिखित कोड का उपयोग करें। यह मेरे लिए काम किया

फ्लोट रोटेशन = 30.0 एफ;

 Bitmap bitmap = your bitmap 
    Rect rect = new Rect(100,100,bitmap.width, bitmap.height); 
    Matrix matrix = new Matrix(); 
    float px = rect.exactCenterX(); 
    float py = rect.exactCenterY(); 
    matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); 
    matrix.postRotate(rotation); 
    matrix.postTranslate(px, py); 
    canvas.drawBitmap(bitmap, matrix, null); 
    matrix.reset(); 
    invalidate(); 
+0

यह अच्छा है। इसे 0,0 – MSaudi

+0

हां से शुरू करने के लिए बस छोटे बदलाव किए गए हैं। Rect rect = नया Rect (0, 0, bitmap.width, bitmap.height); – Sakthi

1

यह एकमात्र ऐसा है जो बिना किसी समस्या के मेरे लिए काम करता है।

private Bitmap rotateBitmap(Bitmap bitmap, int rotationAngleDegree){ 

     int w = bitmap.getWidth(); 
     int h = bitmap.getHeight(); 

     int 

newW=w, newH=h; 
    if (rotationAngleDegree==90 || rotationAngleDegree==270){ 
     newW = h; 
     newH = w; 
    } 
    Bitmap rotatedBitmap = Bitmap.createBitmap(newW,newH, bitmap.getConfig()); 
    Canvas canvas = new Canvas(rotatedBitmap); 

    Rect rect = new Rect(0,0,newW, newH); 
    Matrix matrix = new Matrix(); 
    float px = rect.exactCenterX(); 
    float py = rect.exactCenterY(); 
    matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); 
    matrix.postRotate(rotationAngleDegree); 
    matrix.postTranslate(px, py); 
    canvas.drawBitmap(bitmap, matrix, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG)); 
    matrix.reset(); 

    return rotatedBitmap; 
}