2011-10-07 13:11:56 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher2;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
2011-11-03 13:12:50 -07:00
|
|
|
import android.graphics.PorterDuff;
|
2012-10-01 20:31:09 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
2011-10-07 13:11:56 -07:00
|
|
|
import android.graphics.drawable.StateListDrawable;
|
2011-11-03 13:12:50 -07:00
|
|
|
import android.widget.ImageView;
|
2011-10-07 13:11:56 -07:00
|
|
|
|
|
|
|
|
public class HolographicViewHelper {
|
|
|
|
|
|
|
|
|
|
private final Canvas mTempCanvas = new Canvas();
|
|
|
|
|
|
|
|
|
|
private boolean mStatesUpdated;
|
|
|
|
|
private int mHighlightColor;
|
|
|
|
|
|
|
|
|
|
public HolographicViewHelper(Context context) {
|
|
|
|
|
Resources res = context.getResources();
|
|
|
|
|
mHighlightColor = res.getColor(android.R.color.holo_blue_light);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate the pressed/focused states if necessary.
|
|
|
|
|
*/
|
2011-11-03 13:12:50 -07:00
|
|
|
void generatePressedFocusedStates(ImageView v) {
|
|
|
|
|
if (!mStatesUpdated && v != null) {
|
2011-10-07 13:11:56 -07:00
|
|
|
mStatesUpdated = true;
|
2012-09-10 12:46:30 -07:00
|
|
|
Bitmap original = createOriginalImage(v, mTempCanvas);
|
2011-11-21 12:31:42 -08:00
|
|
|
Bitmap outline = createPressImage(v, mTempCanvas);
|
2012-09-10 12:46:30 -07:00
|
|
|
FastBitmapDrawable originalD = new FastBitmapDrawable(original);
|
|
|
|
|
FastBitmapDrawable outlineD = new FastBitmapDrawable(outline);
|
2011-10-07 13:11:56 -07:00
|
|
|
|
|
|
|
|
StateListDrawable states = new StateListDrawable();
|
2012-09-10 12:46:30 -07:00
|
|
|
states.addState(new int[] {android.R.attr.state_pressed}, outlineD);
|
|
|
|
|
states.addState(new int[] {android.R.attr.state_focused}, outlineD);
|
|
|
|
|
states.addState(new int[] {}, originalD);
|
2011-11-03 13:12:50 -07:00
|
|
|
v.setImageDrawable(states);
|
2011-10-07 13:11:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-21 12:31:42 -08:00
|
|
|
* Invalidates the pressed/focused states.
|
|
|
|
|
*/
|
|
|
|
|
void invalidatePressedFocusedStates(ImageView v) {
|
|
|
|
|
mStatesUpdated = false;
|
|
|
|
|
if (v != null) {
|
|
|
|
|
v.invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-10 12:46:30 -07:00
|
|
|
/**
|
|
|
|
|
* Creates a copy of the original image.
|
|
|
|
|
*/
|
|
|
|
|
private Bitmap createOriginalImage(ImageView v, Canvas canvas) {
|
2012-10-01 20:31:09 +02:00
|
|
|
final Drawable d = v.getDrawable();
|
2012-09-10 12:46:30 -07:00
|
|
|
final Bitmap b = Bitmap.createBitmap(
|
2012-10-01 20:31:09 +02:00
|
|
|
d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
2012-09-10 12:46:30 -07:00
|
|
|
|
|
|
|
|
canvas.setBitmap(b);
|
|
|
|
|
canvas.save();
|
2012-10-01 20:31:09 +02:00
|
|
|
d.draw(canvas);
|
2012-09-10 12:46:30 -07:00
|
|
|
canvas.restore();
|
|
|
|
|
canvas.setBitmap(null);
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-21 12:31:42 -08:00
|
|
|
/**
|
|
|
|
|
* Creates a new press state image which is the old image with a blue overlay.
|
2011-10-07 13:11:56 -07:00
|
|
|
* Responsibility for the bitmap is transferred to the caller.
|
|
|
|
|
*/
|
2011-11-21 12:31:42 -08:00
|
|
|
private Bitmap createPressImage(ImageView v, Canvas canvas) {
|
2012-10-01 20:31:09 +02:00
|
|
|
final Drawable d = v.getDrawable();
|
2011-10-07 13:11:56 -07:00
|
|
|
final Bitmap b = Bitmap.createBitmap(
|
2012-10-01 20:31:09 +02:00
|
|
|
d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
2011-10-07 13:11:56 -07:00
|
|
|
|
|
|
|
|
canvas.setBitmap(b);
|
|
|
|
|
canvas.save();
|
2012-10-01 20:31:09 +02:00
|
|
|
d.draw(canvas);
|
2011-10-07 13:11:56 -07:00
|
|
|
canvas.restore();
|
2011-11-03 13:12:50 -07:00
|
|
|
canvas.drawColor(mHighlightColor, PorterDuff.Mode.SRC_IN);
|
2011-10-07 13:11:56 -07:00
|
|
|
canvas.setBitmap(null);
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
}
|