服务器之家

服务器之家 > 正文

Android应用开发之简易、大气音乐播放器实现专辑倒影效果

时间:2021-04-08 15:30     来源/作者:itheimafeng

今天要实现的功能是实现专辑倒影效果,这个功能已经属于图像处理方面的了,对图像处理我不怎么在行,等一下会介绍一个很实用的工具类,专门用来进行图像处理的。这个工具类不是我写的,我只是拿来用到自己的项目当中,这已经足够了,我已经完美实现我想要的效果。

效果图:

Android应用开发之简易、大气音乐播放器实现专辑倒影效果<

Android应用开发之简易、大气音乐播放器实现专辑倒影效果

一个很有用的工具类

/simplebeautymusicplayer/src/com/wwj/sb/utils/imageutil.java

?
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package com.wwj.sb.utils;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmap.compressformat;
import android.graphics.bitmap.config;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.colormatrix;
import android.graphics.colormatrixcolorfilter;
import android.graphics.lineargradient;
import android.graphics.matrix;
import android.graphics.paint;
import android.graphics.porterduff;
import android.graphics.porterduff.mode;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.rectf;
import android.graphics.shader;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
/**
 * 图片工具类
 * @author wwj
 * 2013/7/3
 */
public class imageutil {
  /**图片的八个位置**/
  public static final int top = 0;      //上 
  public static final int bottom = 1;     //下
  public static final int left = 2;      //左
  public static final int right = 3;     //右
  public static final int left_top = 4;    //左上
  public static final int left_bottom = 5//左下
  public static final int right_top = 6;   //右上
  public static final int right_bottom = 7//右下
  /**
   * 图像的放大缩小方法
   * @param src    源位图对象
   * @param scalex  宽度比例系数
   * @param scaley  高度比例系数
   * @return 返回位图对象
   */
  public static bitmap zoombitmap(bitmap src, float scalex, float scaley) {
    matrix matrix = new matrix();
    matrix.setscale(scalex, scaley);
    bitmap t_bitmap = bitmap.createbitmap(src, 0, 0, src.getwidth(), src.getheight(), matrix, true);
    return t_bitmap;
  }
  /**
   * 图像放大缩小--根据宽度和高度
   * @param src
   * @param width
   * @param height
   * @return
   */
  public static bitmap zoombimtap(bitmap src, int width, int height) {
    return bitmap.createscaledbitmap(src, width, height, true);
  }
  /**
   * 将drawable转为bitmap对象
   * @param drawable
   * @return
   */
  public static bitmap drawabletobitmap(drawable drawable) {
    return ((bitmapdrawable)drawable).getbitmap();
  }
  /**
   * 将bitmap转换为drawable对象
   * @param bitmap
   * @return
   */
  public static drawable bitmaptodrawable(bitmap bitmap) {
    drawable drawable = new bitmapdrawable(bitmap);
    return drawable;
  }
  /**
   * bitmap转byte[]
   * @param bitmap
   * @return
   */
  public static byte[] bitmaptobyte(bitmap bitmap) {
    bytearrayoutputstream out = new bytearrayoutputstream();
    bitmap.compress(bitmap.compressformat.png, 100, out);
    return out.tobytearray();
  }
  /**
   * byte[]转bitmap
   * @param data
   * @return
   */
  public static bitmap bytetobitmap(byte[] data) {
    if(data.length != 0) {
      return bitmapfactory.decodebytearray(data, 0, data.length);
    }
    return null;
  }
  /**
   * 绘制带圆角的图像
   * @param src
   * @param radius
   * @return
   */
  public static bitmap createroundedcornerbitmap(bitmap src, int radius) {
    final int w = src.getwidth();
    final int h = src.getheight();
    // 高清量32位图
    bitmap bitmap = bitmap.createbitmap(w, h, config.argb_8888);
    paint paint = new paint();
    canvas canvas = new canvas(bitmap);
    canvas.drawargb(0, 0, 0, 0);
    paint.setcolor(0xff424242);
    // 防止边缘的锯齿
    paint.setfilterbitmap(true);
    rect rect = new rect(0, 0, w, h);
    rectf rectf = new rectf(rect);
    // 绘制带圆角的矩形
    canvas.drawroundrect(rectf, radius, radius, paint);
    // 取两层绘制交集,显示上层
    paint.setxfermode(new porterduffxfermode(porterduff.mode.src_in));
    // 绘制图像
    canvas.drawbitmap(src, rect, rect, paint);
    return bitmap;
  }
  /**
   * 创建选中带提示图片
   * @param context
   * @param srcid
   * @param tipid
   * @return
   */
  public static drawable createselectedtip(context context, int srcid, int tipid) {
    bitmap src = bitmapfactory.decoderesource(context.getresources(), srcid);
    bitmap tip = bitmapfactory.decoderesource(context.getresources(), tipid);
    final int w = src.getwidth();
    final int h = src.getheight();
    bitmap bitmap = bitmap.createbitmap(w, h, config.argb_8888);
    paint paint = new paint();
    canvas canvas = new canvas(bitmap);
    //绘制原图
    canvas.drawbitmap(src, 0, 0, paint);
    //绘制提示图片
    canvas.drawbitmap(tip, (w - tip.getwidth()), 0, paint);
    return bitmaptodrawable(bitmap);
  }
  /**
   * 带倒影的图像
   * @param src
   * @return
   */
  public static bitmap createreflectionbitmap(bitmap src) {
    // 两个图像间的空隙
    final int spacing = 4;
    final int w = src.getwidth();
    final int h = src.getheight();
    // 绘制高质量32位图
    bitmap bitmap = bitmap.createbitmap(w, h + h / 2 + spacing, config.argb_8888);
    // 创建燕x轴的倒影图像
    matrix m = new matrix();
    m.setscale(1, -1);
    bitmap t_bitmap = bitmap.createbitmap(src, 0, h / 2, w, h / 2, m, true);
    canvas canvas = new canvas(bitmap);
    paint paint = new paint();
    // 绘制原图像
    canvas.drawbitmap(src, 0, 0, paint);
    // 绘制倒影图像
    canvas.drawbitmap(t_bitmap, 0, h + spacing, paint);
    // 线性渲染-沿y轴高到低渲染
    shader shader = new lineargradient(0, h + spacing, 0, h + spacing + h / 2, 0x70ffffff, 0x00ffffff, shader.tilemode.mirror);
    paint.setshader(shader);
    // 取两层绘制交集,显示下层。
    paint.setxfermode(new porterduffxfermode(mode.dst_in));
    // 绘制渲染倒影的矩形
    canvas.drawrect(0, h + spacing, w, h + h / 2 + spacing, paint);
    return bitmap;
  }
  /**
   * 独立的倒影图像
   * @param src
   * @return
   */
  public static bitmap createreflectionbitmapforsingle(bitmap src) {
    final int w = src.getwidth();
    final int h = src.getheight();
    // 绘制高质量32位图
    bitmap bitmap = bitmap.createbitmap(w, h / 2, config.argb_8888);
    // 创建沿x轴的倒影图像
    matrix m = new matrix();
    m.setscale(1, -1);
    bitmap t_bitmap = bitmap.createbitmap(src, 0, h / 2, w, h / 2, m, true);
    canvas canvas = new canvas(bitmap);
    paint paint = new paint();
    // 绘制倒影图像
    canvas.drawbitmap(t_bitmap, 0, 0, paint);
    // 线性渲染-沿y轴高到低渲染  
    shader shader = new lineargradient(0, 0, 0, h / 2, 0x70ffffff,
        0x00ffffff, shader.tilemode.mirror);
    paint.setshader(shader);
    // 取两层绘制交集。显示下层。
    paint.setxfermode(new porterduffxfermode(mode.dst_in));
    // 绘制渲染倒影的矩形
    canvas.drawrect(0, 0, w, h / 2, paint);
    return bitmap;
  }
  public static bitmap creategreybitmap(bitmap src) {
    final int w = src.getwidth();
    final int h = src.getheight();
    bitmap bitmap = bitmap.createbitmap(w, h, config.argb_8888);
    canvas canvas = new canvas(bitmap);
    paint paint = new paint();
    // 颜色变换的矩阵
    colormatrix matrix = new colormatrix();
    // saturation 饱和度值,最小可设为0,此时对应的是灰度图;为1表示饱和度不变,设置大于1,就显示过饱和
    matrix.setsaturation(0);
    colormatrixcolorfilter filter = new colormatrixcolorfilter(matrix);
    paint.setcolorfilter(filter);
    canvas.drawbitmap(src, 0, 0, paint);
    return bitmap;
  }
  /**
   * 保存图片
   * @param src
   * @param filepath
   * @param format:[bitmap.compressformat.png,bitmap.compressformat.jpeg]
   * @return
   */
  public static boolean saveimage(bitmap src, string filepath, compressformat format) {
    boolean rs = false;
    file file = new file(filepath);
    try {
      fileoutputstream out = new fileoutputstream(file);
      if(src.compress(format, 100, out)) {
        out.flush();  //写入流
      }
      out.close();
    } catch (filenotfoundexception e) {
      e.printstacktrace();
    } catch (ioexception e) {
      e.printstacktrace();
    }
    return rs;
  }
  /**
   * 添加水印效果
   * @param src    源位图
   * @param watermark 水印
   * @param direction 方向
   * @param spacing 间距
   * @return
   */
  public static bitmap createwatermark(bitmap src, bitmap watermark, int direction, int spacing) {
    final int w = src.getwidth();
    final int h = src.getheight();
    bitmap bitmap = bitmap.createbitmap(w, h, config.argb_8888);
    canvas canvas = new canvas(bitmap);
    canvas.drawbitmap(src, 0, 0, null);
    if(direction == left_top) {
      canvas.drawbitmap(watermark, spacing, spacing, null);
    } else if(direction == left_bottom){
      canvas.drawbitmap(watermark, spacing, h - watermark.getheight() - spacing, null);
    } else if(direction == right_top) {
      canvas.drawbitmap(watermark, w - watermark.getwidth() - spacing, spacing, null);
    } else if(direction == right_bottom) {
      canvas.drawbitmap(watermark, w - watermark.getwidth() - spacing, h - watermark.getheight() - spacing, null);
    }
    return bitmap;
  }
  /**
   * 合成图像
   * @param direction
   * @param bitmaps
   * @return
   */
  public static bitmap composebitmap(int direction, bitmap... bitmaps) {
    if(bitmaps.length < 2) {
      return null;
    }
    bitmap firstbitmap = bitmaps[0];
    for (int i = 0; i < bitmaps.length; i++) {
      firstbitmap = composebitmap(firstbitmap, bitmaps[i], direction);
    }
    return firstbitmap;
  }
  /**
   * 合成两张图像
   * @param firstbitmap
   * @param secondbitmap
   * @param direction
   * @return
   */
  private static bitmap composebitmap(bitmap firstbitmap, bitmap secondbitmap,
      int direction) {
    if(firstbitmap == null) {
      return null;
    }
    if(secondbitmap == null) {
      return firstbitmap;
    }
    final int fw = firstbitmap.getwidth();
    final int fh = firstbitmap.getheight();
    final int sw = secondbitmap.getwidth();
    final int sh = secondbitmap.getheight();
    bitmap bitmap = null;
    canvas canvas = null;
    if(direction == top) {
      bitmap = bitmap.createbitmap(sw > fw ? sw : fw, fh + sh, config.argb_8888);
      canvas = new canvas(bitmap);
      canvas.drawbitmap(secondbitmap, 0, 0, null);
      canvas.drawbitmap(firstbitmap, 0, sh, null);
    } else if(direction == bottom) {
      bitmap = bitmap.createbitmap(fw > sw ? fw : sw, fh + sh, config.argb_8888);
      canvas = new canvas(bitmap);
      canvas.drawbitmap(firstbitmap, 0, 0, null);
      canvas.drawbitmap(secondbitmap, 0, fh, null);
    } else if(direction == left) {
      bitmap = bitmap.createbitmap(fw + sw, sh > fh ? sh : fh, config.argb_8888);
      canvas = new canvas(bitmap);
      canvas.drawbitmap(secondbitmap, 0, 0, null);
      canvas.drawbitmap(firstbitmap, sw, 0, null);
    } else if(direction == right) {
      bitmap = bitmap.createbitmap(fw + sw, fh > sh ? fh : sh,
          config.argb_8888);
      canvas = new canvas(bitmap);
      canvas.drawbitmap(firstbitmap, 0, 0, null);
      canvas.drawbitmap(secondbitmap, fw, 0, null);
    }
    return bitmap;
  }
}

在简、大气音乐播放器程序中的体现

我就定义了一个方法,就把专辑倒影的效果完美实现出来了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
   * 显示专辑封面
   */
  private void showartwork(mp3info mp3info) {
    bitmap bm = mediautil.getartwork(this, mp3info.getid(), mp3info.getalbumid(), true, false);
    //切换播放时候专辑图片出现透明效果
    animation albumanim = animationutils.loadanimation(playeractivity.this, r.anim.album_replace);
    //开始播放动画效果
    musicalbum.startanimation(albumanim);
    if(bm != null) {
      musicalbum.setimagebitmap(bm); //显示专辑封面图片
      musicablumreflection.setimagebitmap(imageutil.createreflectionbitmapforsingle(bm)); //显示倒影
    } else {
      bm = mediautil.getdefaultartwork(this, false);
      musicalbum.setimagebitmap(bm); //显示专辑封面图片
      musicablumreflection.setimagebitmap(imageutil.createreflectionbitmapforsingle(bm)); //显示倒影
    }
  }

以上内容是android应用开发之简单、大气音乐播放器实现专辑倒影效果的全部教程,希望大家喜欢。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部