Android ScaleDrawable

Wesley13
• 阅读 568

顾名思义,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);
    }
}

代码运行结果:

Android ScaleDrawable

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

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

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
android ContextMenu 上下文菜单示例
ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p
东方客主 东方客主
3年前
Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条
问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh
浩浩 浩浩
3年前
【Flutter实战】线性布局(Row、Column)
4.2线性布局(Row和Column)所谓线性布局,即指沿水平或垂直方向排布子组件。Flutter中通过Row和Column来实现线性布局,类似于Android中的LinearLayout控件。Row和Column都继承自Flex,我们将在弹性布局一节中详细介绍Flex。主轴和纵轴对于线性布局,有主轴和纵轴之分,如果
Wesley13 Wesley13
2年前
android Notification 状态栏通知使用示例
ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_
Wesley13 Wesley13
2年前
android WebView 使用实例
主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"
Stella981 Stella981
2年前
Android 复选框 以及回显
activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/
Stella981 Stella981
2年前
Android控件ListView简易使用(使用ArrayAdapter)
<?xmlversion"1.0"encoding"utf8"?<TextViewxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/tv"android:la
Stella981 Stella981
2年前
LISTVIew 加分割线
分割线样式文件:<?xmlversion"1.0"encoding"utf8"?<layerlistxmlns:android"http://schemas.android.com/apk/res/android"<item<
Stella981 Stella981
2年前
Android 控件抖动效果
利用Android自带的动画效果,实现控件的抖动效果,效果资源文件shark.xml<?xmlversion"1.0"encoding"utf8"?<translatexmlns:android"http://schemas.android.com/apk/res/android"android:fr
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"