2013-07-08 4 views
29

मेरे पास एक छोटा प्रश्न है:कैनवास को आकर्षित करने के लिए कोई भी विचार कैसे करें?

मान लीजिए कि मेरे पास एक (म्यूटेबल) बिटमैप है जिसे मुझे संशोधित करने की आवश्यकता है (छवियों, ग्रंथों, आदि ...)।

कैनवास (पेंट, कैनवास, मैट्रिस आदि) के लिए ड्राइंग के लिए कई विशेष कक्षाओं के साथ गड़बड़ करने के बजाय, मैं सोच रहा था कि इस कार्य के लिए एंड्रॉइड के निर्मित कक्षाओं का उपयोग क्यों न करें और केवल अगर मुझे वास्तव में अनुकूलित करने की आवश्यकता है संचालन मैं अभी भी कैनवास का उपयोग कर सकता था?

public void drawViewToBitmap(Bitmap b, View v, Rect rect) { 
    Canvas c = new Canvas(b); 
    // <= use rect to let the view to draw only into this boundary inside the bitmap 
    view.draw(c); 
} 

ऐसी बात संभव है:

तो, उदाहरण के लिए, आदेश (जो निश्चित रूप से, कोई माता पिता है) बिटमैप पर देखने के किसी भी प्रकार को दिखाने के लिए, मैं अगले समारोह कह सकते हैं? शायद यह भी तरीका है कि यह दृश्यों के पीछे काम करता है?

कैनवास के चित्रण और निर्माण के बीच में मुझे क्या लिखना चाहिए?


संपादित करें: मैं अगले कोड की कोशिश की है, लेकिन यह काम नहीं किया:

final int imageSize = 50; 
rect = new Rect(35, 344 , 35 + imageSize, 344 + imageSize); 
final ImageView imageView = new ImageView(mContext); 
imageView.setImageBitmap(bitmap); 
imageView.setScaleType(ScaleType.CENTER_CROP); 
drawFromViewToCanvas(imageView, getRect(), canvas); 

संपादित करें:

public void drawFromViewToCanvas(final View view, final Rect rect, final Canvas canvas) { 
    final int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY); 
    final int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY); 
    view.measure(widthSpec, heightSpec); 
    // Lay the view out with the known dimensions 
    view.layout(0, 0, rect.width(), rect.height()); 
    // Translate the canvas so the view is drawn at the proper coordinates 
    canvas.save(); 
    canvas.translate(rect.left, rect.top); 
    // Draw the View and clear the translation 
    view.draw(canvas); 
    canvas.restore(); 
} 

उपयोग के उदाहरण वहाँ एक है sony's website पर नमूना:

int measureWidth = View.MeasureSpec.makeMeasureSpec(bitmapWidth, View.MeasureSpec.EXACTLY); 
int measuredHeight = View.MeasureSpec.makeMeasureSpec(bitmapHeight, View.MeasureSpec.EXACTLY); 
view.measure(measureWidth, measuredHeight); 
view.layout(0, 0, bitmapWidth, bitmapHeight); 
view.draw(canvas); 

आश्चर्य है कि यह काम करता है।

उत्तर

44

हाँ, आप यह कर सकते हैं। ध्यान रखें, क्योंकि आप इसे एक लेआउट के लिए संलग्न नहीं कर रहे हैं, तो आप इसे निकालने से पहले मैन्युअल रूप से इसे बाहर रखना करने की आवश्यकता होगी:

view.layout(0, 0, viewWidth, viewHeight); 

और जब तक तुम सिर्फ वास्तव में आप उन चौड़ाई और ऊंचाई मापदंडों के लिए क्या चाहते हैं पता , आप भी यह पहली बार को मापने के लिए चाहते हो सकता है:

int widthSpec = MeasureSpec.makeMeasureSpec (ViewGroup.LayoutParams.WRAP_CONTENT, MeasureSpec.UNSPECIFIED; 
int heightSpec = MeasureSpec.makeMeasureSpec (400, MeasureSpec.UNSPECIFIED; 
view.measure(widthSpec, heightSpec); 
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

संपादित करें:

आप पहले से ही चौड़ाई और ऊंचाई जानते हैं कि आप की जरूरत है:

//Lay the view out with the known dimensions 
view.layout (0, 0, rect.width(), rect.height()); 

//Translate the canvas so the view is drawn at the proper coordinates 
canvas.save(); 
canvas.translate(rect.left, rect.top); 

//Draw the View and clear the translation 
view.draw(canvas); 
canvas.restore(); 

फिर से संपादित करें:

हाँ, परीक्षण किया गया। आप इसे स्वयं आज़मा सकते हैं:

public class DrawingActivity extends Activity { 
    public void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //Set a Rect for the 200 x 200 px center of a 400 x 400 px area 
     Rect rect = new Rect(); 
     rect.set(100, 100, 300, 300); 

     //Allocate a new Bitmap at 400 x 400 px 
     Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(bitmap); 

     //Make a new view and lay it out at the desired Rect dimensions 
     TextView view = new TextView(this); 
     view.setText("This is a custom drawn textview"); 
     view.setBackgroundColor(Color.RED); 
     view.setGravity(Gravity.CENTER); 

     //Measure the view at the exact dimensions (otherwise the text won't center correctly) 
     int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY); 
     int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY); 
     view.measure(widthSpec, heightSpec); 

     //Lay the view out at the rect width and height 
     view.layout(0, 0, rect.width(), rect.height()); 

     //Translate the Canvas into position and draw it 
     canvas.save(); 
     canvas.translate(rect.left, rect.top); 
     view.draw(canvas); 
     canvas.restore(); 

     //To make sure it works, set the bitmap to an ImageView 
     ImageView imageView = new ImageView(this); 
     imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     setContentView(imageView); 
     imageView.setScaleType(ImageView.ScaleType.CENTER); 
     imageView.setImageBitmap(bitmap); 
    } 
} 
+0

जिस संदर्भ में मैंने उल्लेख किया है, उस चौड़ाई और ऊंचाई के लिए उपयोग किया जाना चाहिए जिसे दृश्य को आकर्षित करने की अनुमति है। क्या मैं इसकी चौड़ाई और ऊंचाई का उपयोग कर सकता हूं? भी, भाग का स्थान कहां खींचता है? –

+0

यदि आप पहले से ही स्थान और आकार जानते हैं, तो यह बहुत आसान है। संपादन देखें। – kcoppock

+0

क्या आप वाकई काम करते हैं? क्या आपने इसे आजमाया है, उदाहरण के लिए, टेक्स्ट व्यू या इमेज व्यू पर? –

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

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