मेरे पास एक छोटा प्रश्न है:कैनवास को आकर्षित करने के लिए कोई भी विचार कैसे करें?
मान लीजिए कि मेरे पास एक (म्यूटेबल) बिटमैप है जिसे मुझे संशोधित करने की आवश्यकता है (छवियों, ग्रंथों, आदि ...)।
कैनवास (पेंट, कैनवास, मैट्रिस आदि) के लिए ड्राइंग के लिए कई विशेष कक्षाओं के साथ गड़बड़ करने के बजाय, मैं सोच रहा था कि इस कार्य के लिए एंड्रॉइड के निर्मित कक्षाओं का उपयोग क्यों न करें और केवल अगर मुझे वास्तव में अनुकूलित करने की आवश्यकता है संचालन मैं अभी भी कैनवास का उपयोग कर सकता था?
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);
आश्चर्य है कि यह काम करता है।
जिस संदर्भ में मैंने उल्लेख किया है, उस चौड़ाई और ऊंचाई के लिए उपयोग किया जाना चाहिए जिसे दृश्य को आकर्षित करने की अनुमति है। क्या मैं इसकी चौड़ाई और ऊंचाई का उपयोग कर सकता हूं? भी, भाग का स्थान कहां खींचता है? –
यदि आप पहले से ही स्थान और आकार जानते हैं, तो यह बहुत आसान है। संपादन देखें। – kcoppock
क्या आप वाकई काम करते हैं? क्या आपने इसे आजमाया है, उदाहरण के लिए, टेक्स्ट व्यू या इमेज व्यू पर? –