मैं Imageview ऊँचाई और चौड़ाई बदलना चाहता हूँचित्र छवि के आकार के आधार पर प्रोग्राम के रूप में? मेरे पास दो प्रकार के चित्र हैं एक क्षैतिज है और दूसरा एक ऊर्ध्वाधर है। डिफ़ॉल्ट रूप से मैंने ऊर्ध्वाधर चित्रों के लिए अपना xml कोड लिखा है, लेकिन मैं जावा कोड द्वारा क्षैतिज छवियों के लिए छवि दृश्य ऊंचाई और चौड़ाई बदलना चाहता हूं। यह कैसे करना है ?
मेरा xml कोड है
<RelativeLayout
android:paddingTop="47dp"
android:paddingBottom="48dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:id="@+id/flipcardWrapper">
<ImageView
android:orientation="horizontal"
android:layout_width="365dp"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:id="@+id/imgtrans"
android:background="@android:color/transparent" />
और छवि दृश्य की ऊंचाई और चौड़ाई बदलने के लिए मेरा जावा कोड है
imgview = (ImageView) findViewById(R.id.imgtrans);
Drawable d = getResources().getDrawable(R.drawable.image1);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218)
{
imgtrans.getLayoutParams().width=300;
imgtrans.requestLayout();
}
उत्तर:
जवाब के लिए 0 № 1क्षैतिज के लिए दो छवियां सेट करें और एक अन्य ड्रा करने योग्य है। फिर यह सेट की गई छवि के आधार पर स्क्रीन ओरिएंटेशन ढूंढें
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
Drawable d = getResources().getDrawable(R.drawable.imagehorizontal);
//set your other properties
}
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
Drawable d = getResources().getDrawable(R.drawable.imagevertical);
//set your other properties
}
जवाब के लिए 0 № 2
जावा कोड में ViewGroup.LayoutParams.MATCH_PARENT का उपयोग करें।
imgview = (ImageView) findViewById(R.id.imgtrans);
Drawable d = getResources().getDrawable(R.drawable.image1);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218)
{
imgtrans.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
imgtrans.getLayoutParams().height = someValue;
imgtrans.requestLayout();
}