老规矩。右键取得显示不出来的图片地址,利用下载工具下载这个图片。后缀改为rar即可得到源代码项目。
package com.shortcutdock.demo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class QuickShortcut extends ImageView implements OnClickListener{
private Paint mPaint;
private Drawable mDrawable;
public QuickShortcut(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.CYAN);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
float radius = 5.0f * getContext().getResources().getDisplayMetrics().density;
BlurMaskFilter bmFilter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
mPaint.setMaskFilter(bmFilter);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mDrawable = getDrawable();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isPressed() || isFocused()) {
if (mDrawable instanceof BitmapDrawable) {
Bitmap temp = ((BitmapDrawable) mDrawable).getBitmap();
Bitmap mBitmap = temp.extractAlpha(mPaint, null);
int width = getWidth();
int mWidth = mBitmap.getWidth();
int height = getHeight();
int mHeight = mBitmap.getHeight();
canvas.drawBitmap(mBitmap, (width - mWidth) / 2, (height - mHeight) / 2, mPaint);
}
}
}
@Override
public void onClick(View v) {
int[] pos = new int[2];
getLocationOnScreen(pos);
int width = pos[0];
int height = pos[1];
int nWidth = getWidth() + width;
int nHeight = getHeight() + height;
Rect bRect = new Rect(width, height, nWidth, nHeight);
Intent intent = new Intent();
intent.setSourceBounds(bRect);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
postInvalidate();
}
}
package com.shortcutdock.demo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class QuickShortcut extends ImageView implements OnClickListener{
private Paint mPaint;
private Drawable mDrawable;
public QuickShortcut(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.CYAN);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
float radius = 5.0f * getContext().getResources().getDisplayMetrics().density;
BlurMaskFilter bmFilter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
mPaint.setMaskFilter(bmFilter);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mDrawable = getDrawable();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isPressed() || isFocused()) {
if (mDrawable instanceof BitmapDrawable) {
Bitmap temp = ((BitmapDrawable) mDrawable).getBitmap();
Bitmap mBitmap = temp.extractAlpha(mPaint, null);
int width = getWidth();
int mWidth = mBitmap.getWidth();
int height = getHeight();
int mHeight = mBitmap.getHeight();
canvas.drawBitmap(mBitmap, (width - mWidth) / 2, (height - mHeight) / 2, mPaint);
}
}
}
@Override
public void onClick(View v) {
int[] pos = new int[2];
getLocationOnScreen(pos);
int width = pos[0];
int height = pos[1];
int nWidth = getWidth() + width;
int nHeight = getHeight() + height;
Rect bRect = new Rect(width, height, nWidth, nHeight);
Intent intent = new Intent();
intent.setSourceBounds(bRect);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
postInvalidate();
}
}
package com.shortcutdock.demo;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class QuickShortcut extends ImageView implements OnClickListener{
private Paint mPaint;
private Drawable mDrawable;
public QuickShortcut(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.CYAN);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
float radius = 5.0f * getContext().getResources().getDisplayMetrics().density;
BlurMaskFilter bmFilter = new BlurMaskFilter(radius, BlurMaskFilter.Blur.NORMAL);
mPaint.setMaskFilter(bmFilter);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mDrawable = getDrawable();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isPressed() || isFocused()) {
if (mDrawable instanceof BitmapDrawable) {
Bitmap temp = ((BitmapDrawable) mDrawable).getBitmap();
Bitmap mBitmap = temp.extractAlpha(mPaint, null);
int width = getWidth();
int mWidth = mBitmap.getWidth();
int height = getHeight();
int mHeight = mBitmap.getHeight();
canvas.drawBitmap(mBitmap, (width - mWidth) / 2, (height - mHeight) / 2, mPaint);
}
}
}
@Override
public void onClick(View v) {
int[] pos = new int[2];
getLocationOnScreen(pos);
int width = pos[0];
int height = pos[1];
int nWidth = getWidth() + width;
int nHeight = getHeight() + height;
Rect bRect = new Rect(width, height, nWidth, nHeight);
Intent intent = new Intent();
intent.setSourceBounds(bRect);
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
postInvalidate();
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.shortcutdock.demo.ShortcutDock xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:shortcut="http://schemas.android.com/apk/res/com.shortcutdock.demo"
android:layout_gravity="right|center" android:orientation="horizontal" android:id="@+id/shortcut_bar" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal" android:id="@+id/shortcut_dock_3" android:layout_width="fill_parent" android:layout_height="wrap_content">
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_10" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="10" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_11" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="11" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_12" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="12" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_13" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="13" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_14" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="14" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:id="@+id/shortcut_dock_1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_0" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/phone_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="0" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_1" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/contacts_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="1" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_2" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/all_apps_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="2" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_3" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/sms_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="3" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_4" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/browser_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="4" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:id="@+id/shortcut_dock_2" android:layout_width="fill_parent" android:layout_height="wrap_content">
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_5" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="5" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_6" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="6" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_7" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="7" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_8" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="8" />
<com.shortcutdock.demo.QuickShortcut android:layout_gravity="center" android:id="@+id/quick_shortcut_9" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="56.0dip" android:src="@drawable/add_button" android:scaleType="center" android:layout_weight="1.0" shortcut:position="9" />
</LinearLayout>
</com.shortcutdock.demo.ShortcutDock>
value/attrs.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="shortcut">
<attr name="position" format="integer" />
</declare-styleable>
</resources>