Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में ImageView की ऊंचाई और चौड़ाई कैसे प्राप्त करें?

रनटाइम पर ImageView के आयाम प्राप्त करना

यह उदाहरण दर्शाता है कि बटन क्लिक पर ImageView की वास्तविक ऊंचाई और चौड़ाई कैसे प्राप्त करें।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: लेआउट में ImageView, बटन और TextView जोड़ें

res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_marginTop="30dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/image"/>

    <Button
        android:id="@+id/btnCheckSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:textSize="12sp"
        android:textStyle="bold"
        android:text="ImageView आकार जांचें"/>

    <TextView
        android:id="@+id/textView"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="16sp"
        android:textStyle="bold"/>

</LinearLayout>

चरण 3: बटन क्लिक पर आयाम प्राप्त करें

src/MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView textView;
    Button button;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.btnCheckSize);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(String.format("ImageView का आकार:\nऊंचाई: %s\nचौड़ाई: %s", 
                    String.valueOf(imageView.getHeight()), String.valueOf(imageView.getWidth())));
            }
        });
    }
}

महत्वपूर्ण नोट

getWidth() और getHeight() केवल लेआउट पास के बाद सही मान लौटाते हैं। onCreate में ये 0 लौटाएंगे। बटन क्लिक या ViewTreeObserver का उपयोग करें।

ऐप चलाएं

"ImageView आकार जांचें" बटन दबाकर आयाम देखें।

एंड्रॉइड में ImageView की ऊंचाई और चौड़ाई कैसे प्राप्त करें? एंड्रॉइड में ImageView की ऊंचाई और चौड़ाई कैसे प्राप्त करें?

  1. Tkinter में लेबल विजेट की ऊंचाई/चौड़ाई कैसे सेट करें? Tkinter में लेबल विजेट की ऊंचाई/चौड़ाई कैसे सेट करें?

    लेबल विजेट्स का उपयोग एप्लिकेशन में टेक्स्ट और इमेज प्रदर्शित करने के लिए किया जाता है। लेबल विजेट का आकार कई कारकों पर निर्भर करता है जैसे चौड़ाई, ऊंचाई, और लेबल टेक्स्ट का फॉन्ट-साइज। ऊंचाई और चौड़ाई परिभाषित करती है कि लेबल विजेट विंडो में कैसे दिखाई देगा। लेबल विजेट की ऊंचाई और चौड़ाई सेट करने

  1. Tkinter विजेट की चौड़ाई कैसे प्राप्त करें? Tkinter विजेट की चौड़ाई कैसे प्राप्त करें?

    Tkinter विजेट्स को Tkinter एप्लिकेशन विंडो में मौजूद माना जाता है। सभी विजेट्स को पूर्वनिर्धारित प्रॉपर्टी या फ़ंक्शन का उपयोग करके कॉन्फ़िगर और कस्टमाइज़ किया जा सकता है। Tkinter एप्लिकेशन में विजेट की चौड़ाई प्राप्त करने के लिए, हम winfo_width() मेथड का उपयोग कर सकते हैं। यह विजेट की चौड़ाई वापस

  1. Tkinter Widget के Current X और Y Coordinates कैसे प्राप्त करें? Tkinter Widget के Current X और Y Coordinates कैसे प्राप्त करें?

    Tkinter GUI based applications बनाने के लिए व्यापक रूप से उपयोग किया जाता है। इसमें कई toolkits और functions या modules उपलब्ध हैं जिनका उपयोग किसी particular application के विभिन्न attributes को define करने के लिए किया जा सकता है। GUI applications बनाने के लिए यह buttons, text boxes और labels सहित