android显示图片的两种方法

第一种使用xml文件

1.新建android application

2.将要显示的图片img.png放到res/drawable-mdpi目录下

3.打开res/layout目录下的main.xml文件,用如下代码替换

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<ImageView  
		android:id="@+id/imageView"
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:src="@drawable/img" 
    />
</LinearLayout>

运行项目即可


第二中直接在java代码里实现

1.新建android application

2.将要显示的图片img.png放到res/drawable-mdpi目录下

3.打开src目录下的java源文件,写入如下代码

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class ImgShow extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageView imageView = new ImageView(this);
        imageView.setImageBitmap(
        		BitmapFactory.decodeResource(getResources(), 
        		R.drawable.img));
        this.setContentView(imageView);
    }
}
运行即可


如果你想要一个带有图片作为背景的 SeekBar 并设置成圆角,可以通过自定义 Drawable 来实现。具体实现步骤如下: 1. 在 res/drawable 文件夹下创建一个新的 Drawable 文件,例如 seekbar_bg.xml,代码如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="20dp" /> <stroke android:width="2dp" android:color="#000000" /> </shape> ``` 这个 Drawable 代表一个空心圆角为 20dp 的矩形。 2. 在布局文件中,将 SeekBar 的 android:progressDrawable 和 android:thumb 属性设置为自定义的 Drawable,例如: ``` <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:min="0" android:max="100" android:progress="50" android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seekbar_thumb" /> ``` 3. 创建一个新的 Drawable 文件,例如 seekbar_progress.xml,代码如下: ``` <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/seekbar_bg" /> <item android:id="@android:id/progress"> <clip> <bitmap android:src="@drawable/seekbar_progress_bg" android:gravity="left|center_vertical" /> </clip> </item> </layer-list> ``` 其中,@drawable/seekbar_progress_bg 代表你想要作为背景的图片资源。 4. 创建一个新的 Drawable 文件,例如 seekbar_thumb.xml,代码如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="30dp" android:height="30dp" /> <solid android:color="#000000" /> </shape> ``` 这个 Drawable 代表一个黑色的圆形拇指。 这样就可以实现一个带有图片作为背景的 SeekBar 并设置成圆角了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值