2012-04-19 15 views
6

मेरे पास सीट्सपैनल नामक एक कक्षा है जहां मैं ऑन ड्रा विधि में सीटों (drawRect का उपयोग करके) खींचता हूं। ऑनड्रा विधि पैरामीटर के रूप में कैनवास का उपयोग करती है, लेकिन आप कैनवास का आकार कैसे सेट करते हैं? कारण मैं इस सवाल से पूछ रहा हूं क्योंकि यह वर्ग किसी अन्य वर्ग में बढ़ रहा है। मुझे पता है कि कैनवास में फोन की डिफ़ॉल्ट ऊंचाई और चौड़ाई है, लेकिन मुझे इसे छोटा बनाना होगा। मैं यह कैसे कर सकता हूँ?कैनवास आकार कैसे सेट करें?

किसी भी मदद की सराहना की जाएगी।

-Tolga-

+0

क्या आपका मतलब है आप बनाना चाहते करना कैनवास छोटे? आम तौर पर कैनवास आकार एंड्रॉइड द्वारा प्रबंधित किया जाता है, और दृश्य के मापा आकार पर निर्भर करता है। – Greg

+0

ठीक है, लेकिन फिर मैं दृश्य के आकार को कैसे बदलूं? मैं उलझन में हूं, मैं सिर्फ इस वर्ग के आकार को बदलना चाहता हूं। – Xarialon

+0

आपको इसे बढ़ाने की आवश्यकता क्यों है? किसी भी कारण – UVM

उत्तर

3

मैं करने के लिए कोशिश की है बिटमैप के आकार के कैनवास सेट एक साधारण अनुप्रयोग को कार्यान्वित करें जो मुख्य गतिविधि के भीतर एक ब्लैक रेक्ट खींचता है, जिसे बटन दबाकर खींचा जाता है। उदाहरण के लिए, MainActivity में:

private Button button1; 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    button1=(Button)findViewById(R.id.button); 
    button1.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 
      switch(v.getId()){ 
       case R.id.button: 

        LinearLayout ll=(LinearLayout)findViewById(R.id.linearLayout1); 
        System.out.println(ll.getWidth()+" "+ll.getHeight()); 
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ll.getWidth(),ll.getHeight()); 
        YourView yourView = new YourView(getBaseContext()); 
        yourView.setBackgroundColor(Color.WHITE); 
        ll.addView(yourView,params); 
        break; 
      } 

     } 

    }); 

} 

और YourView कक्षा में:

private Bitmap savedBitmap; 
public YourView(Context context) { 
    super(context); 
} 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    System.out.println(canvas.getWidth()+" "+canvas.getHeight()); 

    Paint textPaint = new Paint(); 
    textPaint.setARGB(255, 0, 0, 0); 
    textPaint.setTextAlign(Paint.Align.RIGHT); 
    textPaint.setTextSize(11); 
    textPaint.setTypeface(Typeface.DEFAULT); 

    canvas.drawColor(Color.WHITE); 
    System.out.println(canvas.getWidth()); 
    System.out.println(canvas.getHeight()); 

    canvas.drawRect(200, 20, 500, 100, textPaint); 
} 

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Push the button and draw a Rect" /> 

<Button 
    android:id="@+id/button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 




<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

</LinearLayout> 

+0

मुझे यह पसंद है, लेकिन दुख की बात है कि यह अभी समस्या का समाधान नहीं करता है। मुझे ऑन ड्रा विधि में कैनवास आकार बदलने की जरूरत है। – Xarialon

+0

इस नई विधि को आजमाएं ... – Ant4res

+0

जब मैं इस विधि का उपयोग करता हूं, तो स्क्रीन सफेद हो जाती है ...:/मुझे अब ऑन-ड्रा विधि की सामग्री दिखाई नहीं दे रही है। – Xarialon

1

अपने मामले में लागू नहीं है, लेकिन यह मेरे लिए काम करता

Bitmap animation = BitmapFactory.decodeResource(mContext.getResources(), resourceId, mBitmapOptions); //Get a bitmap from a image file 

// Create a bitmap for the part of the screen that needs updating. 
Bitmap bitmap = Bitmap.createBitmap(animation.getWidth(), animation.getHeight(), BITMAP_CONFIG); 
bitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT); 
Canvas canvas = new Canvas(bitmap); 

यह

+0

यह मदद नहीं करेगा, मुझे इस मुद्दे को हल करने के लिए एक बेहतर विधि का उपयोग करने की आवश्यकता है। – Xarialon

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