点击图片自动变色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | package com.example.imagelTint; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.ColorStateList; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.graphics.drawable.DrawableCompat; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; @SuppressLint ( "AppCompatCustomView" ) public class MyImageView extends ImageButton { public static final int GET_DATA_SUCCESS = 1 ; public static final int NETWORK_ERROR = 2 ; public static final int SERVER_ERROR = 3 ; private Bitmap mBitmap; //子线程不能操作UI,通过Handler设置图片 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what){ case GET_DATA_SUCCESS: Bitmap bitmap = (Bitmap) msg.obj; setImageBitmap(bitmap); break ; case NETWORK_ERROR: Toast.makeText(getContext(), "网络连接失败" ,Toast.LENGTH_LONG); break ; case SERVER_ERROR: Toast.makeText(getContext(), "服务器发生错误" ,Toast.LENGTH_LONG); break ; } } }; public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); initView(); } public MyImageView(Context context) { super (context); initView(); } public MyImageView(Context context, AttributeSet attrs) { super (context, attrs); initView(); } private void initView(){ Drawable src = MyImageView. this .getDrawable(); MyImageView. this .setImageDrawable(tintDrawable(src)); MyImageView. this .setClickable( true ); // 设置可以点击 } private Drawable tintDrawable(Drawable normal) { final Drawable press = DrawableCompat.wrap(normal); final StateListDrawable draw = new StateListDrawable(); draw.addState( new int []{-android.R.attr.state_pressed},getResources().getDrawable(R.drawable.batman)); DrawableCompat.setTint(press, 0x4bFF00FF ); draw.addState( new int []{android.R.attr.state_pressed},normal); return draw; } //设置网络图片 public void setImageURL( final String path) { //开启一个线程用于联网 new Thread() { @Override public void run() { try { //把传过来的路径转成URL URL url = new URL(path); //获取连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //使用GET方法访问网络 connection.setRequestMethod( "GET" ); //超时时间为10秒 connection.setConnectTimeout( 10000 ); //获取返回码 int code = connection.getResponseCode(); if (code == 200 ) { InputStream inputStream = connection.getInputStream(); //使用工厂把网络的输入流生产Bitmap mBitmap = BitmapFactory.decodeStream(inputStream); //利用Message把图片发给Handler Message msg = Message.obtain(); msg.obj = mBitmap; msg.what = GET_DATA_SUCCESS; handler.sendMessage(msg); inputStream.close(); } else { //服务启发生错误 handler.sendEmptyMessage(SERVER_ERROR); } } catch (IOException e) { e.printStackTrace(); //网络连接错误 handler.sendEmptyMessage(NETWORK_ERROR); } } }.start(); } /** * 获取网络图片Bitmap */ public Bitmap getBitmap() { return mBitmap; } } |
修改色调,饱和度,亮度,参考:https://blog.csdn.net/u010542146/article/details/50538335/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术