android.graphics.Path
时间: 2024-06-02 22:11:17 浏览: 15
android.graphics.Path类是Android中的一个图形路径类,它用于描述二维空间中的几何路径。Path可以用来绘制线条、曲线、矩形、圆形等图形。Path类可以通过一些方法实现对路径的操作,如移动、线条、贝塞尔曲线、圆弧等。Path类可以在Canvas上进行绘制,通过调用Canvas的drawPath方法将Path绘制出来。在Android开发中,Path类通常用于自定义View的绘制。
Path类的主要方法包括:
- moveTo(float x, float y):将Path的起点移动到指定坐标。
- lineTo(float x, float y):连接Path的最后一个点到指定坐标。
- quadTo(float x1, float y1, float x2, float y2):绘制二次贝塞尔曲线。
- cubicTo(float x1, float y1, float x2, float y2, float x3, float y3):绘制三次贝塞尔曲线。
- addRect(float left, float top, float right, float bottom, Direction dir):添加一个矩形到Path中。
- addCircle(float x, float y, float radius, Direction dir):添加一个圆形到Path中。
使用Path类可以实现比较复杂的图形绘制,例如绘制带有圆角的矩形、绘制复杂的曲线等。
相关问题
android.graphics.Path取消首尾连接
如果你使用的是 Android 的 `Path` 类来绘制路径,可以通过 `Path` 类的 `setLastPoint()` 方法来取消首尾连接。
具体来说,你可以将路径的最后一个点设置为路径起点,这样路径就不会形成闭环,从而取消首尾连接。示例代码如下:
```java
Path path = new Path();
// 在路径上添加若干个点
// ...
// 取消首尾连接
path.setLastPoint(path.getFirstPoint().x, path.getFirstPoint().y);
```
在上述代码中,`path.getFirstPoint()` 返回路径的起点,然后将其作为路径的最后一个点,即可取消首尾连接。
/packages/apps/Launcher3/src/com/android/launcher3/graphics/IconShapeOverride.java源码
以下是 IconShapeOverride.java 的源代码:
```java
package com.android.launcher3.graphics;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.android.launcher3.Utilities;
import com.android.launcher3.icons.GraphicsUtils;
import com.android.launcher3.icons.IconProvider;
import com.android.launcher3.icons.ShapeData;
import com.android.launcher3.util.MainThreadInitializedObject;
import com.android.launcher3.util.ResourceBasedOverride;
/**
* Provides custom icon shapes based on user preferences.
*/
public class IconShapeOverride extends ResourceBasedOverride {
public static final String KEY_PREFERENCE = "pref_override_icon_shape";
private static final String TAG = "IconShapeOverride";
private final IconProvider mIconProvider;
public IconShapeOverride(Context context) {
this(context, IconProvider.INSTANCE);
}
@VisibleForTesting
public IconShapeOverride(Context context, IconProvider iconProvider) {
super(context, KEY_PREFERENCE);
mIconProvider = iconProvider;
}
/**
* @return the current shape path, or null if not defined.
*/
@Nullable
public ShapeData getShape() {
String pathString = getStringValue();
if (pathString == null) {
return null;
}
try {
return ShapeData.parse(pathString);
} catch (IllegalArgumentException e) {
Log.e(TAG, "Unable to parse shape", e);
return null;
}
}
/**
* @return the current shape path as a {@link Path} instance, or null if not defined.
*/
@Nullable
public Path getShapePath() {
ShapeData data = getShape();
return data != null ? data.getPath() : null;
}
/**
* @return the current shape path bounds, or null if not defined.
*/
@Nullable
public Rect getShapeBounds() {
ShapeData data = getShape();
return data != null ? data.getBounds() : null;
}
/**
* Returns the shape path for the given context, or null if none is specified.
*/
public static Path getShapePath(Resources res, SharedPreferences prefs) {
IconShapeOverride override = new IconShapeOverride(res);
override.setSharedPreferences(prefs);
return override.getShapePath();
}
/**
* Tests whether the current shape is a circle, by checking if all corners of the shape are at
* the same distance from the center.
*/
public boolean isShapeCircle() {
Path shape = getShapePath();
if (shape == null) {
return false;
}
Rect bounds = getShapeBounds();
if (bounds == null) {
return false;
}
Rect outBounds = new Rect();
shape.computeBounds(outBounds, true);
float centerX = bounds.exactCenterX();
float centerY = bounds.exactCenterY();
float radius = Math.max(centerX - bounds.left, centerY - bounds.top);
float maxDeviation = 0;
float[] radii = new float[9];
shape.approximate(1f, radii);
for (int i = 0; i < radii.length; i += 2) {
float deviation = Math.abs(radii[i] - radius);
if (deviation > maxDeviation) {
maxDeviation = deviation;
}
}
return maxDeviation < GraphicsUtils.EPSILON;
}
/**
* Updates the default icon shape, if the user has not overridden it.
*/
public static void updateDefaultShape(Context context) {
SharedPreferences prefs = Utilities.getPrefs(context);
if (prefs.contains(KEY_PREFERENCE)) {
return;
}
IconShapeOverride override = new IconShapeOverride(context);
Path path = override.getDefaultShape();
if (path != null) {
prefs.edit().putString(KEY_PREFERENCE, ShapeData.toString(path)).apply();
}
}
/**
* @return the default shape path for the current device.
*/
@Nullable
public Path getDefaultShape() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return IconShape.applyMaskIfNeeded(mIconProvider.getDeviceProfile(),
mIconProvider.getIconMask());
}
return null;
}
@Override
protected void onValueChanged() {
super.onValueChanged();
mIconProvider.clearCaches();
}
}
```
该类提供了自定义应用图标的形状的功能,它会根据用户的偏好设置提供自定义图标形状。其中包含了获取、设置、更新默认图标形状等方法。此外,还包含一些辅助方法,如测试当前形状是否为圆形。在 Android Oreo 及以上版本中,会调用 IconShape 类提供的方法来获取默认图标形状。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)