因为我们取得专辑图片后 要在指定的区域显示 所以涉及图片的叠加合成处理 具体处理见代码吧
MusicService.java
package com.shop.bluemusicappwidget;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.shop.bluemusicappwidget.R;
import android.R.string;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.MediaStore;
import android.util.Log;
public class MusicService extends Service {
private MediaPlayer player;
private int currindex = 0;
private BroadcastReceiver receiver;
List
package com.shop.bluemusicappwidget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;
import android.widget.RemoteViews;
import com.shop.bluemusicappwidget.R;
public class BlueMusicAppWidgetProvider extends AppWidgetProvider {
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.bluemusic_widget);
Log.v("shopping", "onReceive");
if (action.equals("com.shop.bluemusicappwidget.play")) {
Log.v("shopping", "appwidget_music_pause");
views.setImageViewResource(R.id.control_play,
R.drawable.appwidget_music_pause);
} else if (action.equals("com.shop.bluemusicappwidget.pause")) {
Log.v("shopping", "appwidget_music_play");
views.setImageViewResource(R.id.control_play,
R.drawable.appwidget_music_play);
} else if (action.equals("com.shop.bluemusicappwidget.title")) {
Log.v("shopping", "appwidget_music_title");
views.setTextViewText(R.id.artist,
intent.getStringExtra(Intent.EXTRA_TEXT));
} else if (action.equals("com.shop.bluemusicappwidget.bitmap")) {
Log.v("shopping", "appwidget_music_bitmap");
if (MusicService.bitmapxx != null) {
Log.v("shopping3", "bitmapxx != null");
Bitmap bitmap = getBlueCornerBitmap(context,
MusicService.bitmapxx);
views.setImageViewBitmap(R.id.album_border, bitmap);
}
}
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
mgr.updateAppWidget(new ComponentName(context,
BlueMusicAppWidgetProvider.class), views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Log.v("shopping", "onUpdate");
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.bluemusic_widget);
Intent inext = new Intent("com.shop.bluemusicappwidget.next");
PendingIntent pnext = PendingIntent.getBroadcast(context, 0, inext, 0);
Intent iprevious = new Intent("com.shop.bluemusicappwidget.previous");
PendingIntent pprevious = PendingIntent.getBroadcast(context, 0,
iprevious, 0);
Intent iplay = new Intent("com.shop.bluemusicappwidget.playorpause");
PendingIntent pplay = PendingIntent.getBroadcast(context, 0, iplay, 0);
views.setOnClickPendingIntent(R.id.control_next, pnext);
views.setOnClickPendingIntent(R.id.control_pre, pprevious);
views.setOnClickPendingIntent(R.id.control_play, pplay);
AppWidgetManager mgr = AppWidgetManager.getInstance(context);
mgr.updateAppWidget(new ComponentName(context,
BlueMusicAppWidgetProvider.class), views);
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
Log.v("shopping", "onDisabled");
Intent intent = new Intent(context, MusicService.class);
context.stopService(intent);
}
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.v("shopping", "onEnabled");
Intent intent = new Intent(context, MusicService.class);
context.startService(intent);
}
public Bitmap getBlueCornerBitmap(Context context, Bitmap bitmap) {
// bitmap = bitmapZoomByScale(bitmap, 0.5, 0.5);
// bitmap = rotate(bitmap, -45);
final Bitmap maskBitmap;
maskBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.pip_3_frame);
int w = maskBitmap.getWidth();
int h = maskBitmap.getHeight();
bitmap = Bitmap.createScaledBitmap(bitmap, w, h, false);
// if(frameBitmap == null || frameBitmap.isRecycled() && resIds[0] !=
// -1){
// frameBitmap = BitmapFactory.decodeResource(context.getResources(),
// resIds[0]);
// }
Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int[] picPixels = new int[w * h];
int[] maskPixels = new int[w * h];
bitmap.getPixels(picPixels, 0, w, 0, 0, w, h);
maskBitmap.getPixels(maskPixels, 0, w, 0, 0, w, h);
for (int i = 0; i < maskPixels.length; i++) {
if (maskPixels[i] == 0xff000000) {
picPixels[i] = 0;
} else if (maskPixels[i] == 0) {
// donothing
} else {
// 把mask的a通道应用与picBitmap
maskPixels[i] &= 0xff000000;
maskPixels[i] = 0xff000000 - maskPixels[i];
picPixels[i] &= 0x00ffffff;
picPixels[i] |= maskPixels[i];
}
}
// 生成前置图片添加蒙板后的bitmap:resultBitmap
output.setPixels(picPixels, 0, w, 0, 0, w, h);
canvas.drawBitmap(output, w, h, null);
return output;
}
}
效果图
源码下载:点击打开链接