Android ScaleDrawable

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

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

布局文件:

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 tools:context="zhangphil.app.MainActivity"> 8 9 <ImageView 10 android:layout_width="match_parent" 11 android:layout_height="100dp" 12 android:id="@+id/imageView1" /> 13 14 <ImageView 15 android:layout_width="match_parent" 16 android:layout_height="100dp" 17 android:id="@+id/imageView2" /> 18 19 <ImageView 20 android:layout_width="match_parent" 21 android:layout_height="100dp" 22 android:id="@+id/imageView3" /> 23 24 <ImageView 25 android:layout_width="match_parent" 26 android:layout_height="100dp" 27 android:id="@+id/imageView4" /> 28 29</LinearLayout>

Java代码:

1package zhangphil.app; 2 3import android.graphics.Color; 4import android.graphics.drawable.ColorDrawable; 5import android.graphics.drawable.ScaleDrawable; 6import android.support.v7.app.AppCompatActivity; 7import android.os.Bundle; 8import android.view.Gravity; 9import android.widget.ImageView; 10 11public class MainActivity extends AppCompatActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 ColorDrawable drawable1=new ColorDrawable(Color.RED); 19 drawable1.setLevel(1); 20 ImageView image1= (ImageView) findViewById(R.id.imageView1); 21 image1.setImageDrawable(drawable1); 22 23 ColorDrawable drawable2=new ColorDrawable(Color.YELLOW); 24 drawable2.setLevel(1); 25 ScaleDrawable sd2 = new ScaleDrawable(drawable2, Gravity.LEFT,0.1f,0.0f); 26 ImageView image2= (ImageView) findViewById(R.id.imageView2); 27 image2.setImageDrawable(sd2); 28 29 ColorDrawable drawable3=new ColorDrawable(Color.BLUE); 30 drawable3.setLevel(1); 31 ScaleDrawable sd3 = new ScaleDrawable(drawable3, Gravity.LEFT,0.2f,0.0f); 32 ImageView image3= (ImageView) findViewById(R.id.imageView3); 33 image3.setImageDrawable(sd3); 34 35 ColorDrawable drawable4=new ColorDrawable(Color.GREEN); 36 drawable4.setLevel(1); 37 ScaleDrawable sd4 = new ScaleDrawable(drawable4, Gravity.LEFT,0.3f,0.0f); 38 ImageView image4= (ImageView) findViewById(R.id.imageView4); 39 image4.setImageDrawable(sd4); 40 } 41}

代码运行结果:

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

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

点赞
收藏

评论区

加载中...

相关推荐

android ContextMenu 上下文菜单示例

ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p

android Notification 状态栏通知使用示例

ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_

android WebView 使用实例

主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"

Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh

【Flutter实战】线性布局(Row、Column)

4.2线性布局(Row和Column)所谓线性布局,即指沿水平或垂直方向排布子组件。Flutter中通过Row和Column来实现线性布局,类似于Android中的LinearLayout控件。Row和Column都继承自Flex,我们将在弹性布局一节中详细介绍Flex。主轴和纵轴对于线性布局,有主轴和纵轴之分,如果

Android 复选框 以及回显

activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/