下面列出了怎么用androidx.annotation.Size的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* TargetVersionSdk大于6.0时的权限检查方法
*
* @param context
* @param perms
* @return
*/
public static boolean hasPermissions(@NonNull Context context,
@Size(min = 1) @NonNull String... perms) {
// Always return true for SDK < M, let the system deal with the permissions
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.w(TAG, "hasPermissions: API version < M, returning true by default");
// DANGER ZONE!!! Changing this will break the library.
return true;
}
// Null context may be passed if we have detected Low API (less than M) so getting
// to this point with a null context should not be possible.
if (context == null) {
throw new IllegalArgumentException("Can't check permissions for null context");
}
for (String perm : perms) {
if (ContextCompat.checkSelfPermission(context, perm)
!= PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* android.graphics.Matrixの3x3行列をOpenGLの4x4(列優先)行列に変換する
* (アフィン変換のみ)
* |a11 a12 a13| |0 1 2| |a11 a12 0 a13| |0 4 8 12|
* |a21 a22 a23| |3 4 5| |a21 a22 0 a23| |1 5 9 13|
* |a31 a32 a33| |6 7 8| => | 0 0 1 0| |2 6 10 14|
* |a31 a32 0 a33| |3 7 11 15|
* @param transform
* @param result
* @return
*/
@NonNull
@Size(min=16)
public static float[] toGLMatrix(@NonNull final Matrix transform,
@NonNull @Size(min=16) final float[] result,
@NonNull @Size(min=9) final float[] aMatrix) {
transform.getValues(aMatrix);
result[ 0] = aMatrix[Matrix.MSCALE_X];
result[ 1] = aMatrix[Matrix.MSKEW_Y];
result[ 2] = 0;
result[ 3] = aMatrix[Matrix.MPERSP_0];
result[ 4] = aMatrix[Matrix.MSKEW_X];
result[ 5] = aMatrix[Matrix.MSCALE_Y];
result[ 6] = 0;
result[ 7] = aMatrix[Matrix.MPERSP_1];
result[ 8] = 0;
result[ 9] = 0;
result[10] = 1;
result[11] = 0;
result[12] = aMatrix[Matrix.MTRANS_X];
result[13] = aMatrix[Matrix.MTRANS_Y];
result[14] = 0;
result[15] = aMatrix[Matrix.MPERSP_2];
return result;
}
/**
* OpenGLの4x4(列優先)行列をandroid.graphics.Matrixの3x3行列に変換する
* (アフィン変換のみ)
* @param transform
* @param result
* @param aMatrix
* @return
*/
public static Matrix toAndroidMatrix(
@NonNull @Size(min=16)final float[] transform,
@NonNull final Matrix result,
@NonNull @Size(min=9) final float[] aMatrix) {
aMatrix[Matrix.MSCALE_X] = transform[ 0];
aMatrix[Matrix.MSKEW_Y] = transform[ 1];
aMatrix[Matrix.MPERSP_0] = transform[ 3];
aMatrix[Matrix.MSKEW_X] = transform[ 4];
aMatrix[Matrix.MSCALE_Y] = transform[ 5];
aMatrix[Matrix.MPERSP_1] = transform[ 7];
aMatrix[Matrix.MTRANS_X] = transform[12];
aMatrix[Matrix.MTRANS_Y] = transform[13];
aMatrix[Matrix.MPERSP_2] = transform[15];
result.setValues(aMatrix);
return result;
}
/**
* OpenGL|ESの4x4行列を列優先で文字列化
* @param transform
* @return
*/
public static String toGLMatrixString(
@NonNull @Size(min=16)final float[] transform) {
return "GLMatrix[" +
transform[0] + ", " +
transform[1] + ", " +
transform[2] + ", " +
transform[3] +
"][" +
transform[4] + ", " +
transform[5] + ", " +
transform[6] + ", " +
transform[7] +
"][" +
transform[8] + ", " +
transform[9] + ", " +
transform[10] + ", " +
transform[11] +
"][" +
transform[12] + ", " +
transform[13] + ", " +
transform[14] + ", " +
transform[15] +
']';
}
/**
* Create a new instance of this class by passing in an outer {@link LineString} and optionally
* one or more inner LineStrings contained within a list. Each of these LineStrings should follow
* the linear ring rules.
* <p>
* Note that if a LineString breaks one of the linear ring rules, a {@link RuntimeException} will
* be thrown.
*
* @param outer a LineString which defines the outer perimeter of the polygon
* @param inner one or more LineStrings inside a list representing holes inside the outer
* perimeter
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.0.0
*/
public static Polygon fromOuterInner(@NonNull LineString outer,
@Nullable @Size(min = 1) List<LineString> inner) {
isLinearRing(outer);
List<List<Point>> coordinates = new ArrayList<>();
coordinates.add(outer.coordinates());
// If inner rings are set to null, return early.
if (inner == null || inner.isEmpty()) {
return new Polygon(TYPE, null, coordinates);
}
for (LineString lineString : inner) {
isLinearRing(lineString);
coordinates.add(lineString.coordinates());
}
return new Polygon(TYPE, null, coordinates);
}
/**
* Create an expression to check this column against several values.
* <p>
* SQL: this IN (values...)
*
* @param values The values to test against this column
* @return Expression
*/
@SafeVarargs
@NonNull
@CheckResult
public final Expr in(@NonNull @Size(min = 1) T... values) {
final int length = values.length;
if (length == 0) {
throw new SQLException("Empty IN clause values");
}
final String[] args = new String[length];
final StringBuilder sb = new StringBuilder(6 + (length << 1));
sb.append(" IN (");
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('?');
args[i] = toSqlArg(values[i]);
}
sb.append(')');
return new ExprN(this, sb.toString(), args);
}
@Override
@Size(3)
public Drawable[] getWeatherIcons(WeatherCode code, boolean dayTime) {
if (config.hasWeatherIcons) {
if (config.hasWeatherAnimators) {
return new Drawable[] {
getDrawable(getWeatherIconName(code, dayTime, 1)),
getDrawable(getWeatherIconName(code, dayTime, 2)),
getDrawable(getWeatherIconName(code, dayTime, 3))
};
} else {
return new Drawable[] {getWeatherIcon(code, dayTime), null, null};
}
}
return defaultProvider.getWeatherIcons(code, dayTime);
}
/**
* Create an expression to check this column against several values.
* <p>
* SQL: this IN (values...)
*
* @param values The values to test against this column
* @return Expression
*/
@NonNull
@CheckResult
public final Expr in(@NonNull @Size(min = 1) Collection<T> values) {
final int length = values.size();
if (length == 0) {
throw new SQLException("Empty IN clause values");
}
final String[] args = new String[length];
final StringBuilder sb = new StringBuilder(6 + (length << 1));
sb.append(" IN (");
final Iterator<T> iterator = values.iterator();
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('?');
args[i] = toSqlArg(iterator.next());
}
sb.append(')');
return new ExprN(this, sb.toString(), args);
}
/**
* Create an expression to check this column against several values.
* <p>
* SQL: this NOT IN (values...)
*
* @param values The values to test against this column
* @return Expression
*/
@NonNull
@CheckResult
public final Expr notIn(@NonNull @Size(min = 1) long... values) {
final int length = values.length;
if (length == 0) {
throw new SQLException("Empty IN clause values");
}
final String[] args = new String[length];
final StringBuilder sb = new StringBuilder(10 + (length << 1));
sb.append(" NOT IN (");
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('?');
args[i] = Long.toString(values[i]);
}
sb.append(')');
return new ExprN(this, sb.toString(), args);
}
/**
* Create an expression to check this column against several values.
* <p>
* SQL: this NOT IN (values...)
*
* @param values The values to test against this column
* @return Expression
*/
@NonNull
@CheckResult
public final Expr notIn(@NonNull @Size(min = 1) Collection<T> values) {
final int length = values.size();
if (length == 0) {
throw new SQLException("Empty IN clause values");
}
final String[] args = new String[length];
final StringBuilder sb = new StringBuilder(10 + (length << 1));
sb.append(" NOT IN (");
final Iterator<T> iterator = values.iterator();
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('?');
args[i] = toSqlArg(iterator.next());
}
sb.append(')');
return new ExprN(this, sb.toString(), args);
}
public WindImplementor(@Size(2) int[] canvasSizes) {
this.paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
backgroundColor = Color.rgb(233, 158, 60);
int[] colors = new int[] {
Color.rgb(240, 200, 148),
Color.rgb(237, 178, 100),
Color.rgb(209, 142, 54),};
float[] scales = new float[] {0.6F, 0.8F, 1};
this.winds = new Wind[51];
for (int i = 0; i < winds.length; i ++) {
winds[i] = new Wind(
canvasSizes[0], canvasSizes[1],
colors[i * 3 / winds.length], scales[i * 3 / winds.length]);
}
this.lastDisplayRate = 0;
this.lastRotation3D = INITIAL_ROTATION_3D;
}
/**
* Create an expression to check this column against several values.
* <p>
* SQL: this IN (values...)
*
* @param values The values to test against this column
* @return Expression
*/
@NonNull
@CheckResult
public final Expr in(@NonNull @Size(min = 1) long... values) {
final int length = values.length;
if (length == 0) {
throw new SQLException("Empty IN clause values");
}
final String[] args = new String[length];
final StringBuilder sb = new StringBuilder(6 + (length << 1));
sb.append(" IN (");
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('?');
args[i] = Long.toString(values[i]);
}
sb.append(')');
return new ExprN(this, sb.toString(), args);
}
/**
* Create an expression to check this column against several values.
* <p>
* SQL: this NOT IN (values...)
*
* @param values The values to test against this column
* @return Expression
*/
@SafeVarargs
@NonNull
@CheckResult
public final Expr notIn(@NonNull @Size(min = 1) T... values) {
final int length = values.length;
if (length == 0) {
throw new SQLException("Empty IN clause values");
}
final String[] args = new String[length];
final StringBuilder sb = new StringBuilder(10 + (length << 1));
sb.append(" NOT IN (");
for (int i = 0; i < length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append('?');
args[i] = toSqlArg(values[i]);
}
sb.append(')');
return new ExprN(this, sb.toString(), args);
}
/**
* constructor to create a new ConfirmLayoutTemplate.
* @param text confirmation description
* @param actions what actions to take for the two buttons
*/
public ConfirmLayoutTemplate(
@NonNull String text,
@NonNull @Size(2) List<ClickActionForTemplateMessage> actions
) {
super(Type.CONFIRM);
this.text = text;
this.actions = actions;
}
@Size(2) /* width, height. */
public static int[] getScreenSize(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager manager = (WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
if (manager != null) {
manager.getDefaultDisplay().getRealMetrics(metrics);
return new int[] {metrics.widthPixels, metrics.heightPixels};
} else {
metrics = context.getResources().getDisplayMetrics();
return new int[] {metrics.widthPixels, metrics.heightPixels};
}
}
private CondomContext(final CondomCore condom, final @Nullable Context app_context, final @Nullable @Size(max=16) String tag) {
super(condom.mBase);
mCondom = condom;
mApplicationContext = app_context != null ? app_context : this;
mBaseContext = new Lazy<Context>() { @Override protected Context create() {
return new PseudoContextImpl(CondomContext.this);
}};
TAG = CondomCore.buildLogTag("Condom", "Condom.", tag);
}
private static void loadImage(Context context, ImageView view,
@NonNull String url, @Nullable DrawableRequestBuilder thumbnailRequest,
@Size(2) @Px int[] size,
@Nullable BitmapTransformation[] ts, @Nullable OnLoadImageListener l) {
view.setTag(R.id.tag_item_image_fade_in_flag, true);
if (ts == null) {
ts = new BitmapTransformation[] {new NullTransformation(context)};
}
Glide.with(getValidContext(context))
.load(ensureUrl(url))
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.override(size[0], size[1])
.thumbnail(thumbnailRequest)
.animate(v -> {
Boolean fadeInFlag = (Boolean) v.getTag(R.id.tag_item_image_fade_in_flag);
if (fadeInFlag == null || fadeInFlag) {
v.setTag(R.id.tag_item_image_fade_in_flag, false);
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "alpha", 0f, 1f);
animator.setDuration(300);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.start();
}
}).transform(ts)
.listener(new BaseRequestListener<>(l))
.into(view);
}
/**
* クオータニオンとして取得(4番目の成分は1)
* @param result nullなら新規生成して返す, nullでないなら4要素以上必要
* @return
*/
public float[] getQuat(@Nullable @Size(min=4) final float[] result) {
final float[] q = result != null ? result : new float[4];
q[0] = x;
q[1] = y;
q[2] = z;
q[3] = 1;
return q;
}
/**
* クオータニオンをセット(4番目の成分は無視される)
* @param q 4要素以上必要
* @return
*/
public Vector setQuat(@NonNull @Size(min=4) final float[] q) {
x = q[0];
y = q[1];
z = q[2];
return this;
}
/**
* トランスフォームマトリックスを設定する
* @param transform nullまたは要素数が16未満なら単位行列が設定される
*/
@NonNull
@Override
public final GLViewTransformer setTransform(@Nullable @Size(min=16) final float[] transform) {
if (DEBUG) Log.v(TAG, "setTransform:" + Arrays.toString(transform));
if ((transform != null) && (transform.length >= 16)) {
System.arraycopy(transform, 0, mTransform, 0, 16);
} else {
Matrix.setIdentityM(mTransform, 0);
}
internalSetTransform(mTransform);
calcValues(mTransform);
return this;
}
/**
* デフォルトのトランスフォームマトリックスを設定
* @param transform nullなら単位行列になる
* @return
*/
@NonNull
@Override
public GLViewTransformer setDefault(@NonNull @Size(min=16) final float[] transform) {
System.arraycopy(transform, 0, mDefaultTransform, 0, 16);
return this;
}
/**
* Matrixからtranslate/scale/rotateの値を計算する
* @param transform
* @return
*/
protected void calcValues(@NonNull @Size(min=16) final float[] transform) {
if (DEBUG) Log.v(TAG, "calcValues:" + MatrixUtils.toGLMatrixString(transform));
// mTransform.getValues(work);
// mCurrentTransX = work[Matrix.MTRANS_X];
// mCurrentTransY = work[Matrix.MTRANS_Y];
// mCurrentScaleX = work[Matrix.MSCALE_X];
// mCurrentScaleY = MatrixUtils.getScale(work);
// mCurrentRotate = MatrixUtils.getRotate(work);
if (DEBUG) Log.v(TAG, String.format("calcValues:tr(%fx%f),scale(%f,%f),rot=%f",
mCurrentTransX, mCurrentTransY,
mCurrentScaleX, mCurrentScaleY,
mCurrentRotate));
}
/**
* コンストラクタ
* @param maxClientVersion
* @param sharedContext
* @param flags
* @param width コンテキスト用のオフスクリーンの幅
* @param height コンテキスト用のオフスクリーンの高さ
*/
public GLContext(final int maxClientVersion,
@Nullable final EGLBase.IContext sharedContext, final int flags,
@Size(min=1) final int width, @Size(min=1) final int height) {
mMaxClientVersion = maxClientVersion;
mSharedContext = sharedContext;
mFlags = flags;
mMasterWidth = width > 0 ? width : 1;
mMasterHeight = height > 0 ? height : 1;
}
/**
* 倍速播放
*
* @param speed 倍速播放,默认为1
* @param pitch 音量缩放,默认为1,修改会导致声音变调
*/
public void setSpeed(@Size(min = 0) float speed, @Size(min = 0) float pitch) {
PlaybackParameters playbackParameters = new PlaybackParameters(speed, pitch);
mSpeedPlaybackParameters = playbackParameters;
if (mInternalPlayer != null) {
mInternalPlayer.setPlaybackParameters(playbackParameters);
}
}
@Override
@Size(3)
public Drawable[] getWeatherIcons(WeatherCode code, boolean dayTime) {
return new Drawable[] {
getDrawable(getWeatherIconName(code, dayTime, 1)),
getDrawable(getWeatherIconName(code, dayTime, 2)),
getDrawable(getWeatherIconName(code, dayTime, 3))
};
}
@Override
@Size(3)
public Animator[] getWeatherAnimators(WeatherCode code, boolean dayTime) {
return new Animator[] {
getAnimator(getWeatherAnimatorName(code, dayTime, 1)),
getAnimator(getWeatherAnimatorName(code, dayTime, 2)),
getAnimator(getWeatherAnimatorName(code, dayTime, 3))
};
}
public static Bitmap loadBitmap(Context context, @DrawableRes int id, @Nullable @Size(2) int[] size)
throws ExecutionException, InterruptedException {
if (size == null) {
size = new int[] {Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL};
}
return Glide.with(getValidContext(context))
.load(id)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(size[0], size[1])
.get();
}
public void setAnimatableIcon(@NonNull @Size(3) Drawable[] drawables,
@NonNull @Size(3) Animator[] animators) {
endAnimators();
for (int i = 0; i < drawables.length; i ++) {
iconImageViews[i].setImageDrawable(drawables[i]);
iconImageViews[i].setVisibility(drawables[i] == null ? GONE : VISIBLE);
iconAnimators[i] = animators[i];
if (iconAnimators[i] != null) {
iconAnimators[i].setTarget(iconImageViews[i]);
}
}
}
@Size(2)
public static int[] getMeasureSize(int measureWidth, float w, float h) {
return new int[] {
measureWidth,
(int) (measureWidth * h / w)
};
}
@Override
public void draw(@Size(2) int[] canvasSizes, Canvas canvas,
float displayRate, float scrollRate, float rotation2D, float rotation3D) {
if (displayRate >=1) {
canvas.drawColor(backgroundColor);
} else {
canvas.drawColor(
ColorUtils.setAlphaComponent(
backgroundColor,
(int) (displayRate * 255)));
}
if (scrollRate < 1) {
if (thunder != null) {
canvas.drawColor(
Color.argb(
(int) (displayRate * (1 - scrollRate) * thunder.alpha * 255),
thunder.r,
thunder.g,
thunder.b));
}
for (Star s : stars) {
paint.setColor(s.color);
paint.setAlpha((int) (displayRate * (1 - scrollRate) * s.alpha * 255));
canvas.drawCircle(s.centerX, s.centerY, s.radius, paint);
}
for (Cloud c : clouds) {
paint.setColor(c.color);
paint.setAlpha((int) (displayRate * (1 - scrollRate) * c.alpha * 255));
canvas.drawCircle(c.centerX, c.centerY, c.radius, paint);
}
}
}