简体   繁体   English

更改可绘制资源的背景图像

[英]Change background image of drawable resource

Basically I created a background.xml to add the background image to preserve the aspect ratio. 基本上,我创建了background.xml来添加背景图像以保留宽高比。 Then I set the background of the ImageView to @drawable/background . 然后我将ImageView的背景设置为@drawable/background

How could I change the background image from the Activity? 如何更改活动中的背景图像?

In activity_main.xml: 在activity_main.xml中:

<ImageView
            android:scaleType="centerCrop"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:id="@+id/imageView1"
            android:layout_alignTop="@+id/scrollView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="@drawable/background"
            android:src="@drawable/gradient"/>

In background.xml: 在background.xml中:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="fill"
    android:src="@drawable/image" />

@drawable/image is the actual .jpg file @drawable/image是实际的.jpg文件

You can just set imageview programmatically like this 您可以像这样以编程方式设置imageview

ImageView image;
//initialization
image.setImageResource(int);

// where int is resource like R.drawable.something // int是R.drawable.something之类的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM