Introduce the examples of pointer shape API.

Change-Id: Ib0be39d560fbc00db661f639a02c4df2e2c1867b
This commit is contained in:
Jun Mukai
2015-11-17 17:24:15 -08:00
committed by Vladislav Kaznacheev
parent 7f5ff73972
commit 6cb3668322
11 changed files with 556 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2016 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.example.android.apis.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.widget.Button;
public class LivePointerIconButton extends Button {
public LivePointerIconButton(Context context) {
this(context, null);
}
public LivePointerIconButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LivePointerIconButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public LivePointerIconButton(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
int cursorSize = getHeight();
Bitmap bitmap = Bitmap.createBitmap(cursorSize, cursorSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
final int strokeWidth = 4;
paint.setStrokeWidth(strokeWidth);
// Draw a large circle filling the bitmap.
final int outerCenterX = cursorSize / 2;
final int outerCenterY = cursorSize / 2;
final int outerRadius = cursorSize / 2 - strokeWidth;
canvas.drawCircle(outerCenterX, outerCenterY, outerRadius, paint);
// Compute relative offset of the mouse pointer from the view center.
// It should be between -0.5 and 0.5.
final float relativeX = (x / getWidth()) - 0.5f;
final float relativeY = (y / getHeight()) - 0.5f;
// Draw a smaller circle inside the large circle, offset towards the center of the view.
final int innerCenterX = (int) (cursorSize * (1 - relativeX) / 2);
final int innerCenterY = (int) (cursorSize * (1 - relativeY) / 2);
final int innerRadius = cursorSize / 6;
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// Fill the inner circle if the mouse button is down.
paint.setStyle(Paint.Style.FILL);
}
canvas.drawCircle(innerCenterX, innerCenterY, innerRadius, paint);
final int hotSpotX = bitmap.getWidth() / 2;
final int hotSpotY = bitmap.getHeight() / 2;
return PointerIcon.createCustomIcon(bitmap, hotSpotX, hotSpotY);
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2016 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.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
/**
* A gallery of the different styles of buttons.
*/
public class PointerShapes extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pointer_shapes);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2016 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.example.android.apis.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.widget.Button;
import com.example.android.apis.R;
public class ResourcePointerIconButton extends Button {
private PointerIcon mCustomIcon;
public ResourcePointerIconButton(Context context) {
this(context, null);
}
public ResourcePointerIconButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ResourcePointerIconButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public ResourcePointerIconButton(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
if (mCustomIcon == null) {
mCustomIcon = PointerIcon.loadCustomIcon(getResources(), R.drawable.custom_pointer_icon);
}
return mCustomIcon;
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2016 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.example.android.apis.view;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.widget.Button;
import com.example.android.apis.R;
public class StaticPointerIconButton extends Button {
PointerIcon mCustomIcon;
public StaticPointerIconButton(Context context) {
this(context, null);
}
public StaticPointerIconButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public StaticPointerIconButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public StaticPointerIconButton(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
if (mCustomIcon == null) {
Drawable d = getContext().getDrawable(R.drawable.smile);
final BitmapDrawable bitmapDrawable = (BitmapDrawable) d;
final int hotSpotX = d.getIntrinsicWidth() / 2;
final int hotSpotY = d.getIntrinsicHeight() / 2;
mCustomIcon = PointerIcon.createCustomIcon(
bitmapDrawable.getBitmap(), hotSpotX, hotSpotY);
}
return mCustomIcon;
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2016 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.example.android.apis.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.PointerIcon;
import android.widget.Button;
public class SystemPointerIconButton extends Button {
public SystemPointerIconButton(Context context) {
this(context, null);
}
public SystemPointerIconButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SystemPointerIconButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public SystemPointerIconButton(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public PointerIcon getPointerIcon(MotionEvent event, float x, float y) {
final int minX = getWidth() / 4;
final int maxX = getWidth() - minX;
final int minY = getHeight() / 4;
final int maxY = getHeight() - minY;
int style;
if ((x < minX && y < minY) || (x > maxX && y > maxY)) {
// Top/left or bottom/right corner.
style = PointerIcon.STYLE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW;
} else if ((x < minX && y > maxY) || (x > maxX && y < minY)) {
// Top/rightor bottom/left corner.
style = PointerIcon.STYLE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW;
} else if (x < minX || x > maxX) {
// Left or right edge.
style = PointerIcon.STYLE_HORIZONTAL_DOUBLE_ARROW;
} else if (y < minY || y > maxY) {
// Top or bottom edge edge.
style = PointerIcon.STYLE_VERTICAL_DOUBLE_ARROW;
} else {
// Everything else (the middle).
style = PointerIcon.STYLE_ALL_SCROLL;
}
return PointerIcon.getSystemIcon(getContext(), style);
}
}