Android ScaleDrawable

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

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

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView1" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView2" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView3" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/imageView4" />

</LinearLayout>



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);
    }
}



代码运行结果:



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


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



转载于:https://www.cnblogs.com/hehehaha/p/6147271.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值