Android实现炫酷的星空变幻效果

2024-05-29 07:18

本文主要是介绍Android实现炫酷的星空变幻效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

二话不说,先上效果图:


这个图是什么意思呢,有没有看到一直在变颜色啊,有没有很像星云变幻呢,有没有很炫,快来看看怎么实现的吧!


这是我们要被处理的原图,实现方式就是通过不断的改变这张图的色相从而达到效果:


贴布局文件:


<?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" ><ImageViewandroid:id="@+id/image"android:layout_width="match_parent"android:layout_height="wrap_content"android:src="@drawable/nightfall_starlight_panoramic9"android:scaleType="centerCrop" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"android:padding="10dp"android:visibility="visible" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="色相"android:textAppearance="?android:attr/textAppearanceSmall" /><SeekBarandroid:id="@+id/hue"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="饱和度"android:textAppearance="?android:attr/textAppearanceSmall" /><SeekBarandroid:id="@+id/saturation"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="明度"android:textAppearance="?android:attr/textAppearanceSmall" /><SeekBarandroid:id="@+id/lum"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal" ><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="start"android:text="start"android:visibility="visible" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="reset"android:text="reset"android:visibility="visible" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="stop"android:text="stop"android:visibility="visible" /></LinearLayout></LinearLayout>


贴实现代码:

package com.sahadev;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;import com.lidroid.xutils.view.annotation.ViewInject;public class MainActivity3 extends Activity implements OnSeekBarChangeListener, Runnable {@ViewInject(R.id.hue)SeekBar hue;@ViewInject(R.id.saturation)SeekBar saturation;@ViewInject(R.id.lum)SeekBar lum;@ViewInject(R.id.image)ImageView image;// 颜色的最大值255,中间值127private final static int MAX_VALUE = 255, MID_VALUE = 127;// 临时 色相,饱和度,明度private float mHue, mSaturation, mLum;// 被处理的图像private Bitmap bitmap;// 临时变换值private int tempValue = MID_VALUE;// 运行标志位private boolean runFlag;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.primary_color);com.lidroid.xutils.ViewUtils.inject(this);// 将图片缩放到屏幕的适合尺寸bitmap = MainActivity.scaleImage(this, R.drawable.nightfall_starlight_panoramic9);// 事件绑定hue.setOnSeekBarChangeListener(this);saturation.setOnSeekBarChangeListener(this);lum.setOnSeekBarChangeListener(this);// 进行初始化状态hue.setMax(MAX_VALUE);saturation.setMax(MAX_VALUE);lum.setMax(MAX_VALUE);reset(null);image.setImageBitmap(bitmap);}/** 进行循环任务* * @see java.lang.Runnable#run()*/@Overridepublic void run() {hue.setProgress(tempValue++);if (tempValue == MAX_VALUE) {tempValue = 0;}if (!runFlag) {return;}image.postDelayed(this, 10);}/*** 停止循环* * @param view*/public void stop(View view) {runFlag = false;}/*** 开始循环滚动* * @param view*/public void start(View view) {runFlag = true;image.postDelayed(this, 100);}/*** 重置* * @param view*/public void reset(View view) {hue.setProgress(MID_VALUE);saturation.setProgress(MID_VALUE);lum.setProgress(MID_VALUE);tempValue = MID_VALUE;}/** 根据Seekbar的进度控制图片的效果*/@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {switch (seekBar.getId()) {case R.id.hue:mHue = (progress - MID_VALUE) * 1.0f / MID_VALUE * 180;break;case R.id.saturation:mSaturation = progress * 1.0f / MID_VALUE;break;case R.id.lum:mLum = progress * 1.0f / MID_VALUE;break;default:break;}image.setImageBitmap(ImageHelper.handleImageEffect(bitmap, mHue, mSaturation, mLum));}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {}public static class ImageHelper {/*** 处理图像* * @param bitmap*            原图* @param degrees*            色相值* @param sat*            饱和度值* @param lum*            明度值* @return 处理后的图像* */public static Bitmap handleImageEffect(Bitmap bitmap, float degrees, float sat, float lum) {Bitmap temp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(temp);Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);// 设置色相ColorMatrix hueMatrix = new ColorMatrix();hueMatrix.setRotate(0, degrees);hueMatrix.setRotate(1, degrees);hueMatrix.setRotate(2, degrees);// 设置饱和度ColorMatrix saturationMatrix = new ColorMatrix();saturationMatrix.setSaturation(sat);// 设置明度ColorMatrix lumMatrix = new ColorMatrix();lumMatrix.setScale(lum, lum, lum, 1);// 融合ColorMatrix imageMatrix = new ColorMatrix();imageMatrix.postConcat(lumMatrix);imageMatrix.postConcat(saturationMatrix);imageMatrix.postConcat(hueMatrix);// 给paint设置颜色属性paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));// 绘制canvas.drawBitmap(bitmap, 0, 0, paint);return temp;}}}

在onCreate方法里刚开始会调用一个scaleImage的方法,该方法可以出门左转参见如何适配APP引导页的文章,有详细介绍。

地址:http://blog.csdn.net/sahadev_/article/details/48475217

没想到安卓提供的图片处理效果这么强大,也了解到原来图片处理是通过矩阵来算的,其它知识请自行查阅。


快试一下效果吧!

这篇关于Android实现炫酷的星空变幻效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1012998

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1