Skip to content

Commit

Permalink
优化3D效果
Browse files Browse the repository at this point in the history
  • Loading branch information
CNCoderX committed Oct 17, 2017
1 parent 6fdf6ac commit eae6e7b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
compileSdkVersion 25
buildToolsVersion "25.0.3"

defaultConfig {
applicationId "com.cncoderx.test.wheelview"
minSdkVersion 15
targetSdkVersion 24
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void onChanged(WheelView wheel, int oldIndex, int newIndex) {
mYear = 1980;
mMonth = 1;
mDay = 1;
// updateDayEntries();
updateDayEntries();
updateTextView();
}

Expand Down
2 changes: 1 addition & 1 deletion library/bintray.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.jfrog.bintray'
def siteUrl = 'https://github.com/CNCoderX/WheelView'
def gitUrl = 'https://github.com/CNCoderX/WheelView.git'

version = "1.0"
version = "1.1.0"
group = "com.cncoderx.wheelview"
def libName = "WheelView"
def libDesc = "like ios wheel selector"
Expand Down
18 changes: 13 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
compileSdkVersion 25
buildToolsVersion "25.0.3"

defaultConfig {
minSdkVersion 15
targetSdkVersion 24
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName "1.1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// compileOptions {
// sourceCompatibility JavaVersion.VERSION_1_8
// targetCompatibility JavaVersion.VERSION_1_8
// }
lintOptions {
abortOnError false
checkReleaseBuilds false
}
}

dependencies {
Expand Down
26 changes: 16 additions & 10 deletions library/src/main/java/com/cncoderx/wheelview/Wheel3DView.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ protected void drawItem(Canvas canvas, int index, int offset) {
// 滚动的距离映射到y轴的长度
float translateY = (float) (Math.sin(angle) * r);

float scaleX = (float) Math.cos(angle / 3);
float scaleOffset = (float) Math.abs(Math.sin(angle / 2) * (itemHeight - getLineSpace()));

// 折射偏移量x
float refractX = getRefractX();
// float refractX = getRefractX();

int clipLeft = getPaddingLeft();
int clipRight = getWidth() - getPaddingRight();
Expand All @@ -113,7 +114,7 @@ protected void drawItem(Canvas canvas, int index, int offset) {
if (Math.abs(range) <= 0) {
mPaint.setColor(getSelectedColor());
canvas.save();
canvas.translate(refractX, 0);
// canvas.translate(refractX, 0);
canvas.clipRect(clipLeft, upperLimit, clipRight, lowerLimit);
drawText(canvas, text, translateX, translateY, rotate, 1);
canvas.restore();
Expand All @@ -122,41 +123,41 @@ protected void drawItem(Canvas canvas, int index, int offset) {
else if (range > 0 && range < itemHeight) {
mPaint.setColor(getSelectedColor());
canvas.save();
canvas.translate(refractX, 0);
// canvas.translate(refractX, 0);
canvas.clipRect(clipLeft, upperLimit, clipRight, lowerLimit);
drawText(canvas, text, translateX, translateY, rotate, 1);
canvas.restore();

mPaint.setColor(getUnselectedColor());
canvas.save();
canvas.clipRect(clipLeft, lowerLimit, clipRight, clipBottom);
drawText(canvas, text, translateX, translateY, rotate, scaleX);
drawText(canvas, text, translateX, translateY, rotate, scaleOffset);
canvas.restore();
}
// 绘制与上分界线相交的文字
else if (range < 0 && range > -itemHeight) {
mPaint.setColor(getSelectedColor());
canvas.save();
canvas.translate(refractX, 0);
// canvas.translate(refractX, 0);
canvas.clipRect(clipLeft, upperLimit, clipRight, lowerLimit);
drawText(canvas, text, translateX, translateY, rotate, 1);
canvas.restore();

mPaint.setColor(getUnselectedColor());
canvas.save();
canvas.clipRect(clipLeft, clipTop, clipRight, upperLimit);
drawText(canvas, text, translateX, translateY, rotate, scaleX);
drawText(canvas, text, translateX, translateY, rotate, scaleOffset);
canvas.restore();
} else {
mPaint.setColor(getUnselectedColor());
canvas.save();
canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
drawText(canvas, text, translateX, translateY, rotate, scaleX);
drawText(canvas, text, translateX, translateY, rotate, scaleOffset);
canvas.restore();
}
}

private void drawText(Canvas canvas, CharSequence text, float translateX, float translateY, float rotate, float scale) {
private void drawText(Canvas canvas, CharSequence text, float translateX, float translateY, float rotate, float scaleOffset) {
mCamera.save();
mCamera.rotateX(rotate);
mCamera.getMatrix(mMatrix);
Expand All @@ -166,7 +167,12 @@ private void drawText(Canvas canvas, CharSequence text, float translateX, float
mMatrix.preTranslate(-centerX, -centerY);
mMatrix.postTranslate(centerX + translateX, centerY + translateY);

canvas.scale(scale, 1, centerX, centerY);
final int width = canvas.getWidth();
final int height = canvas.getHeight();
float scaleX = (width - scaleOffset) / width;
float scaleY = (height - scaleOffset) / height;
canvas.scale(scaleX, scaleY, centerX, centerY);

canvas.concat(mMatrix);
canvas.drawText(text, 0, text.length(), centerX, centerY - baseline, mPaint);
}
Expand Down

1 comment on commit eae6e7b

@654833536
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good!

Please sign in to comment.