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;
|
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;
|
2011-11-03 13:12:50 -07:00
|
|
|
Bitmap outline = createGlowingOutline(v, mTempCanvas);
|
2011-10-07 13:11:56 -07:00
|
|
|
FastBitmapDrawable d = new FastBitmapDrawable(outline);
|
|
|
|
|
|
|
|
|
|
StateListDrawable states = new StateListDrawable();
|
|
|
|
|
states.addState(new int[] {android.R.attr.state_pressed}, d);
|
|
|
|
|
states.addState(new int[] {android.R.attr.state_focused}, d);
|
2011-11-03 13:12:50 -07:00
|
|
|
states.addState(new int[] {}, v.getDrawable());
|
|
|
|
|
v.setImageDrawable(states);
|
2011-10-07 13:11:56 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a new bitmap to be used as the object outline, e.g. to visualize the drop location.
|
|
|
|
|
* Responsibility for the bitmap is transferred to the caller.
|
|
|
|
|
*/
|
2011-11-03 13:12:50 -07:00
|
|
|
private Bitmap createGlowingOutline(ImageView v, Canvas canvas) {
|
2011-10-07 13:11:56 -07:00
|
|
|
final int padding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
|
|
|
|
|
final Bitmap b = Bitmap.createBitmap(
|
|
|
|
|
v.getWidth() + padding, v.getHeight() + padding, Bitmap.Config.ARGB_8888);
|
|
|
|
|
|
|
|
|
|
canvas.setBitmap(b);
|
|
|
|
|
canvas.save();
|
2011-11-03 13:12:50 -07:00
|
|
|
v.getDrawable().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;
|
|
|
|
|
}
|
|
|
|
|
}
|