Changed up layout (again)
Removed ColorSelectionListener for now
Added AlphaView and AlphaSelectionListener for selecting alpha (transparency)
Removed all divisions from onDraw() calls
This commit is contained in:
artdeell 2022-04-21 23:08:46 +03:00
parent 8590786d1f
commit 9937413d24
7 changed files with 101 additions and 29 deletions

View File

@ -0,0 +1,5 @@
package net.kdt.pojavlaunch.colorselector;
public interface AlphaSelectionListener {
void onAlphaSelected(int alpha, boolean tapping);
}

View File

@ -0,0 +1,72 @@
package net.kdt.pojavlaunch.colorselector;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import net.kdt.pojavlaunch.Tools;
import top.defaults.checkerboarddrawable.CheckerboardDrawable;
public class AlphaView extends View {
Drawable mCheckerboardDrawable = CheckerboardDrawable.create();
Paint mShaderPaint = new Paint();
Paint mBlackPaint;
RectF mViewSize = new RectF(0,0,0,0);
AlphaSelectionListener mAlphaSelectionListener;
int mSelectedAlpha;
float mAlphaDiv; // for quick pos->alpha multiplication
float mScreenDiv; // for quick alpha->pos multiplication
float mHeightThird; // 1/3 of the view size for cursor
public AlphaView(Context ctx, AttributeSet attrs) {
super(ctx,attrs);
mBlackPaint = new Paint();
mBlackPaint.setStrokeWidth(Tools.dpToPx(3));
mBlackPaint.setColor(Color.BLACK);
}
public void setAlpha(int alpha) {
if(mAlphaSelectionListener != null) mAlphaSelectionListener.onAlphaSelected(mSelectedAlpha,false);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
mSelectedAlpha = (int) (mAlphaDiv * event.getX());
if(mAlphaSelectionListener != null) mAlphaSelectionListener.onAlphaSelected(mSelectedAlpha,true);
invalidate();
return true;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mViewSize.right = w;
mViewSize.bottom = h;
float h2 = mViewSize.bottom / 2f;
mShaderPaint.setShader(new LinearGradient(0,h2,w,h2, Color.BLACK, 0, Shader.TileMode.REPEAT));
mAlphaDiv = 255f / mViewSize.right;
mScreenDiv = mViewSize.right / 255f;
mHeightThird = mViewSize.bottom / 3f;
}
@Override
protected void onDraw(Canvas canvas) {
Log.i("onDraw","onDraw");
mCheckerboardDrawable.draw(canvas);
canvas.drawRect(mViewSize, mShaderPaint);
float linePos = mSelectedAlpha * mScreenDiv;
Log.i("onDraw",linePos+"");
canvas.drawLine(linePos, 0 ,linePos, mHeightThird, mBlackPaint);
canvas.drawLine(linePos, mHeightThird * 2 ,linePos, mViewSize.bottom, mBlackPaint);
}
}

View File

@ -1,5 +0,0 @@
package net.kdt.pojavlaunch.colorselector;
public interface ColorSelectionListener {
void onColorSelected(int color);
}

View File

@ -12,11 +12,10 @@ import android.widget.TextView;
import net.kdt.pojavlaunch.R;
public class ColorSelector implements HueSelectionListener, RectangleSelectionListener{
public class ColorSelector implements HueSelectionListener, RectangleSelectionListener, AlphaSelectionListener{
HueView mHueView;
SVRectangleView mLuminosityIntensityView;
EditText mTextView;
ColorSelectionListener mColorSelectionListener;
float[] mHueTemplate = new float[] {0,1,1};
float[] mHsvSelected = new float[] {360,1,1};
AlertDialog mDialog;
@ -28,11 +27,11 @@ public class ColorSelector implements HueSelectionListener, RectangleSelectionLi
mLuminosityIntensityView = view.findViewById(R.id.color_selector_rectangle_view);
mLuminosityIntensityView.setRectSelectionListener(this);
mLuminosityIntensityView.setLuminosityIntensity(1,1);
mTextView = view.findViewById(R.id.color_selector_hex_edit);
mColorSelectionListener = (color)->{
mTextView.setTextColor(color);
};
//mTextView = view.findViewById(R.id.color_selector_hex_edit);
bldr.setView(view);
bldr.setPositiveButton(android.R.string.ok,(dialog,which)->{
});
mDialog = bldr.create();
}
@ -54,7 +53,12 @@ public class ColorSelector implements HueSelectionListener, RectangleSelectionLi
dispatchColorChange(Color.HSVToColor(mHsvSelected),tapping);
}
@Override
public void onAlphaSelected(int alpha, boolean tapping) {
}
protected void dispatchColorChange(int color, boolean tapping) {
if(mColorSelectionListener != null) mColorSelectionListener.onColorSelected(Color.HSVToColor(mHsvSelected));
}
}

View File

@ -21,6 +21,7 @@ public class HueView extends View {
Paint blackPaint = new Paint();
float mSelectionHue;
float mWidthHueRatio;
float mHueWidthRatio;
float mWidth;
float mHeight;
float mHeightThird;
@ -52,7 +53,7 @@ public class HueView extends View {
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mGamma, 0, 0 ,null);
float linePos = mSelectionHue / mWidthHueRatio;
float linePos = mSelectionHue * mHueWidthRatio;
canvas.drawLine(linePos, 0 ,linePos, mHeightThird, blackPaint);
canvas.drawLine(linePos, mHeightThird * 2 ,linePos, mHeight, blackPaint);
}
@ -72,7 +73,8 @@ public class HueView extends View {
Paint paint = new Paint();
Canvas canvas = new Canvas(mGamma);
mWidthHueRatio = 360/ mWidth;
float[] hsvFiller = new float[] {0,1,1};
mHueWidthRatio = mWidth / 360;
float[] hsvFiller = new float[] {0, 1, 1};
for(float i = 0; i < mWidth; i++) {
hsvFiller[0] = i * mWidthHueRatio;
paint.setColor(Color.HSVToColor(hsvFiller));

View File

@ -82,9 +82,8 @@ public class SVRectangleView extends View {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(mViewSize, mColorPaint);
drawCrosshair(canvas, mViewSize.right * mFingerPosX, mViewSize.bottom * mFingerPosY);
if(mSvRectangle != null)
canvas.drawBitmap(mSvRectangle, 0,0, null);
drawCrosshair(canvas, mViewSize.right * mFingerPosX, mViewSize.bottom * mFingerPosY);
}
@Override

View File

@ -1,14 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<net.kdt.pojavlaunch.colorselector.HueView
android:id="@+id/color_selector_hue_view"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_height="25dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
@ -19,25 +18,21 @@
<net.kdt.pojavlaunch.colorselector.SVRectangleView
android:id="@+id/color_selector_rectangle_view"
android:layout_width="0dp"
android:layout_height="250dp"
android:layout_width="200dp"
android:layout_height="150dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:focusable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/color_selector_hex_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<net.kdt.pojavlaunch.colorselector.AlphaView
android:id="@+id/color_selector_alpha_view"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:digits="0123456789ABCDEF"
android:ems="10"
android:inputType="textPersonName|textCapCharacters"
app:layout_constraintEnd_toEndOf="@+id/color_selector_hue_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/color_selector_hue_view" />