Android Drawable Resources系列6:<transition>

本文介绍如何使用TransitionDrawable实现两张图片间的淡入淡出效果。通过XML配置图片资源及位置,并通过startTransition()和reverseTransition()方法控制过渡动画。适用于希望了解此功能实现细节的开发者。

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

定义:可以控制两张图片之间的淡入淡出效果(不超过两张),通过startTransition()执行,reverseTransition()撤销.

用法:

<?xml version="1.0" encoding="utf-8"?>
<transition
xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />
</transition>

属性作用
android:drawable图片资源
android:top、android:right、android:bottom、android:left距离各边的距离

官方示例:

XML file saved at res/drawable/transition.xml:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>
This layout XML applies the drawable to a View:

<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />
And the following code performs a 500ms transition from the first item to the second:

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);


效果:

xml文件:
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/reasource_drawable_mn"/>
    <item android:drawable="@mipmap/reasource_drawable_mn2"/>
</transition>

Activity中:
final TransitionDrawable drawable = (TransitionDrawable) img_transition.getDrawable();
        btn_startTransition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawable.startTransition(2000);
            }
        });
        btn_reverseTransition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawable.reverseTransition(2000);
            }
        });

效果:





CommandInvokationFailure: Failed to re-package resources. D:\Android\AndroidSDK\build-tools\25.0.3\aapt.exe package --auto-add-overlay -v -f -m -J "gen" -M "AndroidManifest.xml" -S "res" -I "D:/Android/AndroidSDK\platforms\android-27\android.jar" -F bin/resources.ap_ --extra-packages example.administrator.myapplication -S "D:\03-game\Gme\GameBoxNewDT-UI_DB2 - Android\Temp\StagingArea\android-libraries\app-debug\res" stderr[ AndroidManifest.xml:17: Tag <provider> attribute authorities has invalid character '$'. ] stdout[ Configurations: (default) Files: xml\provider_paths.xml Src: () D:\03-game\Gme\GameBoxNewDT-UI_DB2 - Android\Temp\StagingArea\android-libraries\app-debug\res\xml\provider_paths.xml AndroidManifest.xml Src: () AndroidManifest.xml Resource Dirs: Type xml xml\provider_paths.xml Src: () D:\03-game\Gme\GameBoxNewDT-UI_DB2 - Android\Temp\StagingArea\android-libraries\app-debug\res\xml\provider_paths.xml Including resources from package: D:\Android\AndroidSDK\platforms\android-27\android.jar applyFileOverlay for drawable trying overlaySet Key=app_banner.png trying overlaySet Key=app_icon.png applyFileOverlay for layout applyFileOverlay for anim applyFileOverlay for animator applyFileOverlay for interpolator applyFileOverlay for transition applyFileOverlay for xml applyFileOverlay for raw applyFileOverlay for color applyFileOverlay for menu applyFileOverlay for mipmap Processing image: res\drawable-xhdpi\app_banner.png Processing image: res\drawabProceslsing eim-alged:p reis\\darpawable-mpdpi_\appi_cicon.png on.png Processing image: res\drawable-hdpi\app_icon.png (processed image res\drawable-mdpi\app_icon.png: 99% size of source) Processing image: res\drawable-xhdpi\app_icon.png (processed image res\drawable-ldpi\app_icon.png: 99% size of source) Processing image: res\drawable-xxhdpi\app_icon.png (processed image res\drawable-hdpi\app_icon.png: 99% size of source) Processing image: res\drawable-xxxhdpi\app_icon.png (processed image res\drawable-xhdpi\app_icon.png: 99% size of source) (processed image res\drawable-xhdpi\app_banner.png: 93% size of source) (processed image res\drawable-xxhdpi\app_icon.png: 98% size of source) (processed image res\drawable-xxxhdpi\app_icon.png: 98% size of source) (new resource id app_banner from xhdpi-v4\drawable\app_banner.png #generated) (new resource id app_icon from ldpi-v4\drawable\app_icon.png #generated) (new resource id app_icon from mdpi-v4\drawable\app_icon.png #generated) (new resource id app_icon from hdpi-v4\drawable\app_icon.png #generated) (new resource id app_icon from xhdpi-v4\drawable\app_icon.png #generated) (new resource id app_icon from xxhdpi-v4\drawable\app_icon.png #generated) (new resource id app_icon from xxxhdpi-v4\drawable\app_icon.png #generated) (new resource id provider_paths from D:\03-game\Gme\GameBoxNewDT-UI_DB2 - Android\Temp\StagingArea\android-libraries\app-debug\res\xml\provider_paths.xml) ] exit code: 1 UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.Android.PostProcessor.Tasks.TasksCommon.Exec (System.String command, System.String args, System.String workingdir, System.String errorMsg) UnityEditor.Android.PostProcessor.Tasks.BuildResources.CompileResources (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.Android.PostProcessor.Tasks.BuildResources.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
最新发布
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值