Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90d8ca6

Browse files
author
lin.zhang01
committed
使用数学向量原理实现view上的贴纸功能
0 parents  commit 90d8ca6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1435
-0
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# kdiff3 ignore
2+
*.orig
3+
# maven ignore
4+
target/
5+
# eclipse ignore
6+
.settings/
7+
.project
8+
.classpath
9+
# idea ignore
10+
.idea/
11+
*.ipr
12+
*.iml
13+
*.iws
14+
# temp ignore
15+
*.log
16+
*.cache
17+
*.diff
18+
*.patch
19+
*.tmp
20+
# system ignore
21+
.DS_Store
22+
Thumbs.db
23+
# package ignore (optional)
24+
# *.jar
25+
# *.war
26+
# *.zip
27+
# *.tar
28+
# *.tar.gz
29+
local.properties
30+
build/
31+
app/build/
32+
.gradle/
33+
**/*.iml

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 26
5+
defaultConfig {
6+
applicationId "com.zhanglin.labelview"
7+
minSdkVersion 16
8+
targetSdkVersion 26
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'com.android.support:appcompat-v7:26.1.0'
24+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25+
testImplementation 'junit:junit:4.12'
26+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
27+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
28+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.zhanglin.labelview;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.zhanglin.labelview", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.zhanglin.labelview">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.zhanglin.labelview;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
7+
import com.zhanglin.labelview.view.LabelTextView;
8+
9+
public class MainActivity extends AppCompatActivity {
10+
private boolean isShow = true;
11+
12+
@Override
13+
protected void onCreate(Bundle savedInstanceState) {
14+
super.onCreate(savedInstanceState);
15+
setContentView(R.layout.activity_main);
16+
findViewById(R.id.label).setOnClickListener(new View.OnClickListener() {
17+
@Override
18+
public void onClick(View v) {
19+
isShow = !isShow;
20+
((LabelTextView) v).showOrHideLabel(isShow);
21+
}
22+
});
23+
}
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.zhanglin.labelview.util;
2+
3+
import android.graphics.Point;
4+
5+
public class MyPointF extends android.graphics.PointF implements Cloneable {
6+
public float z;
7+
8+
public MyPointF(float x, float y) {
9+
this(x, y, 0f);
10+
}
11+
12+
public MyPointF(float x, float y, float z) {
13+
super(x, y);
14+
this.z = z;
15+
}
16+
17+
public MyPointF moveTo(Phasor v, float len) {
18+
return moveTo(v, len, false);
19+
}
20+
21+
public MyPointF moveTo(Phasor v, float len, boolean isDirectionPhasor) {
22+
if (isDirectionPhasor) {
23+
this.x = this.x + v.x * len;
24+
this.y = this.y + v.y * len;
25+
} else {
26+
Phasor v0 = v.getDirectionPhasor();
27+
this.x = this.x + v0.x * len;
28+
this.y = this.y + v0.y * len;
29+
}
30+
return this;
31+
}
32+
33+
public MyPointF moveTo(Phasor v) {
34+
this.x = this.x + v.x;
35+
this.y = this.y + v.y;
36+
return this;
37+
}
38+
39+
40+
public Point toPoint() {
41+
return new Point(Math.round(this.x), Math.round(this.y));
42+
}
43+
44+
public MyPointF clone() {
45+
return new MyPointF(x, y);
46+
}
47+
48+
@Override
49+
public String toString() {
50+
String str = "(%f,%f,%f)";
51+
return String.format(str, x, y, z);
52+
}
53+
54+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.zhanglin.labelview.util;
2+
3+
public class Phasor implements Cloneable {
4+
float x;
5+
float y;
6+
//z 屏幕内外方向
7+
float z;
8+
9+
public Phasor() {
10+
x = 0;
11+
y = 0;
12+
z = 0;
13+
}
14+
15+
public Phasor(float dx, float dy) {
16+
this(dx, dy, 0f);
17+
}
18+
19+
public Phasor(float dx, float dy, float dz) {
20+
this.x = dx;
21+
this.y = dy;
22+
this.z = dz;
23+
}
24+
25+
//p1->p2
26+
public Phasor(MyPointF p1, MyPointF p2) {
27+
this.x = p2.x - p1.x;
28+
this.y = p2.y - p1.y;
29+
this.z = p2.z - p1.z;
30+
}
31+
32+
//向量点积
33+
public float point(Phasor p) {
34+
return p.x * x + p.y * y + p.z * z;
35+
}
36+
37+
//向量叉积
38+
public Phasor x(Phasor v) {
39+
float x1 = y * v.z - z * v.y;
40+
float y1 = z * v.x - x * v.z;
41+
float z1 = x * v.y - y * v.x;
42+
return new Phasor(x1, y1, z1);
43+
}
44+
45+
public Phasor getDirectionPhasor() {
46+
float d = (float) Math.sqrt(this.x * this.x + this.y * this.y);
47+
Phasor p = new Phasor(x / d, y / d);
48+
return p;
49+
}
50+
51+
public float length() {
52+
return (float) Math.sqrt(x * x + y * y + z * z);
53+
}
54+
55+
public void reverse() {
56+
this.x = -this.x;
57+
this.y = -this.y;
58+
this.z = -this.z;
59+
}
60+
61+
public double angleBy(Phasor v) {
62+
float cosx = getDirectionPhasor().point(v.getDirectionPhasor());
63+
double radians = Math.acos(cosx);
64+
return radians;
65+
}
66+
67+
@Override
68+
public String toString() {
69+
String str = "(x=%f,y=%f)";
70+
return String.format(str, x, y);
71+
}
72+
73+
public Phasor clone() {
74+
return new Phasor(x, y, z);
75+
}
76+
77+
//在同屏幕旋转
78+
public void rotate(double radians) {
79+
double sint = Math.sin(radians);
80+
double cost = Math.cos(radians);
81+
float x1 = (float) (x * cost + y * sint);
82+
float y1 = (float) (y * cost - x * sint);
83+
this.x = x1;
84+
this.y = y1;
85+
}
86+
}

0 commit comments

Comments
 (0)