android6.0 多用户 csdn,Android ScaleDrawable

本文通过一个实例展示了如何在Android中使用ScaleDrawable进行drawable的宽度缩放。在一个线性布局中,放置了四个ImageView,分别设置不同比例的ScaleDrawable,实现了从红色到绿色的线性渐变缩放效果。尽管预期是同时缩放宽度和高度,但实际结果只缩放了宽度。这为理解Android中的Drawable缩放提供了一个基础示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

顾名思义,Android ScaleDrawable实现一个drawable的缩放。写一个例子。

一个线性布局,垂直放几个ImageView,然后依次缩放若干个ScaleDrawable。

布局文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="zhangphil.app.MainActivity">

android:layout_width="match_parent"

android:layout_height="100dp"

android:id="@+id/imageView1" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:id="@+id/imageView2" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:id="@+id/imageView3" />

android:layout_width="match_parent"

android:layout_height="100dp"

android:id="@+id/imageView4" />

Java代码:

package zhangphil.app;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.graphics.drawable.ScaleDrawable;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.Gravity;

import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ColorDrawable drawable1=new ColorDrawable(Color.RED);

drawable1.setLevel(1);

ImageView image1= (ImageView) findViewById(R.id.imageView1);

image1.setImageDrawable(drawable1);

ColorDrawable drawable2=new ColorDrawable(Color.YELLOW);

drawable2.setLevel(1);

ScaleDrawable sd2 = new ScaleDrawable(drawable2, Gravity.LEFT,0.1f,0.0f);

ImageView image2= (ImageView) findViewById(R.id.imageView2);

image2.setImageDrawable(sd2);

ColorDrawable drawable3=new ColorDrawable(Color.BLUE);

drawable3.setLevel(1);

ScaleDrawable sd3 = new ScaleDrawable(drawable3, Gravity.LEFT,0.2f,0.0f);

ImageView image3= (ImageView) findViewById(R.id.imageView3);

image3.setImageDrawable(sd3);

ColorDrawable drawable4=new ColorDrawable(Color.GREEN);

drawable4.setLevel(1);

ScaleDrawable sd4 = new ScaleDrawable(drawable4, Gravity.LEFT,0.3f,0.0f);

ImageView image4= (ImageView) findViewById(R.id.imageView4);

image4.setImageDrawable(sd4);

}

}

代码运行结果:

ce30c500d4363c62f8fd6cb851b88e7d.png

以上ScaleDrawable没有让drawable的高度缩放,只缩放宽度。

附录:

1,《Android ImageView的setImageLevel和level-list使用简介》链接:http://blog.youkuaiyun.com/zhangphil/article/details/48936209



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值