其他分享
首页 > 其他分享> > Android-自定义图像资源的使用(2)(1),面试宝典

Android-自定义图像资源的使用(2)(1),面试宝典

作者:互联网

过渡图像资源的使用


这个图像资源是用来展示图像过渡的,比如一盏灯从不亮到亮的缓慢过渡。

/05_KindOfDrawableUse/res/drawable/lamp_transition.xml

<?xml version="1.0" encoding="utf-8"?>

/05_KindOfDrawableUse/res/layout/cross_fade_res.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:orientation=“vertical” >

<ImageView

android:id="@+id/imageview_lamp"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:src="@drawable/lamp_transition" />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:onClick=“onClick_LampOn”

android:text=“开灯” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:onClick=“onClick_LampOff”

android:text=“关灯” />

/05_KindOfDrawableUse/src/com/wwj/drawable/CrossFadeDrawableRes.java

package com.wwj.drawable;

import android.app.Activity;

import android.graphics.drawable.TransitionDrawable;

import android.os.Bundle;

import android.view.View;

import android.widget.ImageView;

/**

*/

public class CrossFadeDrawableRes extends Activity {

private ImageView ivLamp;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.cross_fade_res);

ivLamp = (ImageView) findViewById(R.id.imageview_lamp);

}

public void onClick_LampOn(View view) {

// 从第一个图像切换到第二个图像。其中使用1秒的时间完成淡入淡出效果

TransitionDrawable drawable = (TransitionDrawable) ivLamp.getDrawable();

drawable.startTransition(1000);

}

public void onClick_LampOff(View view) {

// 从第二个图像切换第一个图像。其中使用1秒的时间完成淡入淡出效果

TransitionDrawable drawable = (TransitionDrawable) ivLamp.getDrawable();

drawable.reverseTransition(1000);

}

}

效果图如下:

嵌入图像资源的使用


/05_KindOfDrawableUse/res/drawable/inset.xml

<?xml version="1.0" encoding="utf-8"?>

<inset xmlns:android=“http://schemas.android.com/apk/res/android”

android:drawable="@drawable/logo"

android:insetBottom=“10dp”

android:insetLeft=“10dp”

android:insetRight=“10dp”

android:insetTop=“10dp” >

/05_KindOfDrawableUse/res/layout/inset_res.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical” >

<ImageView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background="@drawable/inset" />

效果图如下:

剪切图像资源的使用


/05_KindOfDrawableUse/res/drawable/clip.xml

<?xml version="1.0" encoding="utf-8"?>

<clip xmlns:android=“http://schemas.android.com/apk/res/android”

android:clipOrientation=“horizontal”

android:drawable="@drawable/progress"

android:gravity=“left” />

/05_KindOfDrawableUse/res/layout/clip_res.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:background="@drawable/background"

android:orientation=“vertical” >

<ImageView

android:id="@+id/image"

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:background="@drawable/clip" />

/05_KindOfDrawableUse/src/com/wwj/drawable/ClipDrawableRes.java

package com.wwj.drawable;

import android.app.Activity;

import android.graphics.drawable.ClipDrawable;

import android.os.Bundle;

import android.widget.ImageView;

/**

*/

public class ClipDrawableRes extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCr
eate(savedInstanceState);

setContentView(R.layout.clip_res);

ImageView imageview = (ImageView) findViewById(R.id.image);

ClipDrawable drawable = (ClipDrawable) imageview.getBackground();

// 截取30%的图像

drawable.setLevel(3000);

}

}

效果图如下:

比例图像资源的使用


/05_KindOfDrawableUse/res/drawable/scale.xml

<?xml version="1.0" encoding="utf-8"?>

<scale xmlns:android=“http://schemas.android.com/apk/res/android”

android:drawable="@drawable/logo"

android:scaleGravity=“center_vertical|center_horizontal”

android:scaleHeight=“80%”

android:scaleWidth=“80%” >

这个比例图片没有效果,不知道为何

外形图像资源的使用


外形图像是用得比较多,可以实现自己想要的效果,比如一个文本框

/05_KindOfDrawableUse/res/drawable/shape.xml

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android=“http://schemas.android.com/apk/res/android”

android:shape=“rectangle” >

<gradient

android:angle=“45”

android:endColor="#80FF00FF"

android:startColor="#FFFF0000" />

<padding

android:bottom=“7dp”

android:left=“7dp”

android:right=“7dp”

android:top=“7dp” />

<stroke

android:width=“2dp”

android:color="#FFF" />

/05_KindOfDrawableUse/res/layout/shape_res.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:orientation=“vertical” >

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_margin=“20dp”

android:background="@drawable/shape"

android:text=“Shape Label” />

效果图如下:

/05_KindOfDrawableUse/res/layout/shape_res.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”

android:orientation=“vertical” >

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_margin=“20dp”

android:background="@drawable/shape"

android:text=“Shape Label” />

效果图如下:

标签:layout,自定义,res,宝典,content,wrap,Android,drawable,android
来源: https://blog.csdn.net/m0_66155658/article/details/122759261