Skip to content

Commit 3443c69

Browse files
committed
fix weather view's bug
1 parent d2eaf9b commit 3443c69

Some content is hidden

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

43 files changed

+366
-48
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
compile 'com.android.support.constraint:constraint-layout:1.0.2'
4444
compile rootProject.ext.dependencies["retrofit"]
4545
compile rootProject.ext.dependencies["rxjava2"]
46+
compile rootProject.ext.dependencies["rxandroid"]
4647
compile rootProject.ext.dependencies["gson"]
4748
compile rootProject.ext.dependencies["retrofit_rxjava"]
4849
compile rootProject.ext.dependencies["retrofit_gson"]
@@ -53,7 +54,6 @@ dependencies {
5354
compile rootProject.ext.dependencies["bugly_sdk"]
5455
compile rootProject.ext.dependencies["bugly_ndk"]
5556
compile rootProject.ext.dependencies["supertextview"]
56-
compile rootProject.ext.dependencies["rxandroid"]
5757
compile rootProject.ext.dependencies["anko"]
5858
compile rootProject.ext.dependencies["log_interceptor"]
5959
if(isLibrary){

app/src/main/AndroidManifest.xml

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
android:supportsRtl="true"
1616
android:theme="@style/AppTheme">
1717
<activity android:name=".MainActivity">
18-
<!--<intent-filter>-->
19-
<!--<action android:name="android.intent.action.MAIN" />-->
20-
21-
<!--<category android:name="android.intent.category.LAUNCHER" />-->
22-
<!--</intent-filter>-->
23-
</activity>
24-
<activity android:name=".FileTestActivity">
2518
<intent-filter>
2619
<action android:name="android.intent.action.MAIN" />
2720

2821
<category android:name="android.intent.category.LAUNCHER" />
2922
</intent-filter>
3023
</activity>
24+
<activity android:name=".FileTestActivity">
25+
<!--<intent-filter>-->
26+
<!--<action android:name="android.intent.action.MAIN" />-->
27+
28+
<!--<category android:name="android.intent.category.LAUNCHER" />-->
29+
<!--</intent-filter>-->
30+
</activity>
3131
<activity
3232
android:name=".weather.WeatherActivity"
3333
android:screenOrientation="portrait" />

app/src/main/java/com/xiasuhuei321/gank_kotlin/GankApplication.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class GankApplication : Application() {
2020
}
2121

2222

23-
val Any.context: Context
23+
val Any.context
2424
get() = GankApplication.mContext!!
2525

2626
val Any.app

app/src/main/java/com/xiasuhuei321/gank_kotlin/MainActivity.kt

+2-20
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package com.xiasuhuei321.gank_kotlin
22

3-
import android.content.Intent
43
import android.os.Bundle
54
import android.support.v7.app.AppCompatActivity
65
import android.view.Window
76
import android.view.WindowManager
8-
import com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
9-
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
107
import com.xiasuhuei321.gank_kotlin.extension.shortToast
11-
import com.xiasuhuei321.weather.TestActivity
128
import kotlinx.android.synthetic.main.activity_main.*
139

1410
class MainActivity : AppCompatActivity() {
15-
lateinit var weatherWv: WeatherView
1611
override fun onCreate(savedInstanceState: Bundle?) {
1712
super.onCreate(savedInstanceState)
1813
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
@@ -23,29 +18,16 @@ class MainActivity : AppCompatActivity() {
2318
weatherIv.setOnClickListener {
2419
shortToast("weather icon be clicked")
2520
}
26-
startActivity(Intent(this, TestActivity::class.java))
27-
weatherWv = WeatherView(this)
21+
// startActivity(Intent(this, TestActivity::class.java))
22+
// weatherWv = WeatherView(this)
2823
}
2924

3025
override fun onResume() {
31-
// weatherWv.setZOrderOnTop(true)
32-
// weatherWv.setZOrderMediaOverlay(false)
33-
// weatherWv.canRun = true
34-
// try {
35-
// weatherWv.lock.notify()
36-
// }catch (e: Exception){}
37-
containerFl.addView(weatherWv)
38-
LogUtil.i("onResume")
3926
super.onResume()
4027
}
4128

4229
override fun onPause() {
4330
super.onPause()
44-
containerFl.removeAllViews()
45-
// weatherWv.setZOrderOnTop(false)
46-
// weatherWv.setZOrderMediaOverlay(true)
47-
// weatherWv.canRun = false
48-
// LogUtil.i("onPause")
4931
}
5032

5133
override fun onDestroy() {

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/WeatherShape.kt

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ abstract class WeatherShape(val start: PointF, val end: PointF) {
7070
* 绘制自身,这里在Shape是非使用的时候进行一些初始化操作
7171
*/
7272
open fun draw(canvas: Canvas) {
73+
// canvas.drawColor(context.resources.getColor(R.color.sky_blue),PorterDuff.Mode.DST_ATOP)
7374
if (!isInUse) {
7475
lastTime += randomPre()
7576
initStyle()

app/src/main/java/com/xiasuhuei321/gank_kotlin/customview/weather/WeatherView.kt

+43-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import android.graphics.Canvas
55
import android.graphics.Color
66
import android.graphics.PixelFormat
77
import android.graphics.PorterDuff
8+
import android.graphics.drawable.GradientDrawable
89
import android.util.AttributeSet
910
import android.view.SurfaceHolder
1011
import android.view.SurfaceView
12+
import com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView.SkyBackground.RAIN_D
1113
import com.xiasuhuei321.gank_kotlin.extension.LogUtil
1214
import java.lang.Exception
1315

@@ -19,6 +21,7 @@ import java.lang.Exception
1921
class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: Int) :
2022
SurfaceView(context, attributeSet, defaultStyle), SurfaceHolder.Callback {
2123
private val TAG = "WeatherView"
24+
private val skyBackgroud = GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, RAIN_D)
2225

2326
constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)
2427

@@ -49,13 +52,20 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
4952
val canvas = holder.lockCanvas()
5053
if (canvas != null) {
5154
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
55+
// 这种方式绘制比较卡,换一种
56+
// canvas.drawColor(context.resources.getColor(R.color.sky_blue), PorterDuff.Mode.DST_OVER)
57+
// 感谢 mixiaoxiao 大神的实现思路
58+
skyBackgroud.setBounds(0, 0, measuredWidth, measuredHeight)
59+
skyBackgroud.draw(canvas)
5260
draw(canvas, type)
5361
}
5462
holder.unlockCanvasAndPost(canvas)
5563
val drawTime = System.currentTimeMillis() - startTime
5664
// 平均16ms一帧才能有顺畅的感觉
5765
if (drawTime < 16) {
5866
Thread.sleep(16 - drawTime)
67+
} else {
68+
LogUtil
5969
}
6070
} catch (e: Exception) {
6171
// e.printStackTrace()
@@ -93,7 +103,7 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
93103
holder.addCallback(this)
94104
holder.setFormat(PixelFormat.TRANSLUCENT)
95105
// initData()
96-
setZOrderOnTop(true)
106+
// setZOrderOnTop(true)
97107
// setZOrderMediaOverlay(true)
98108
thread.start()
99109
}
@@ -124,4 +134,36 @@ class WeatherView(context: Context, attributeSet: AttributeSet?, defaultStyle: I
124134
} catch (e: Exception) {
125135
}
126136
}
137+
138+
object SkyBackground {
139+
val BLACK = intArrayOf(0xff000000.toInt(), 0xff000000.toInt())
140+
// public static final int[] CLEAR_D = new int[] { 0xff3d99c2, 0xff4f9ec5 };
141+
// public static final int[] CLEAR_N = new int[] { 0xff0d1229, 0xff262c42 };
142+
143+
val CLEAR_D = intArrayOf(0xff3d99c2.toInt(), 0xff4f9ec5.toInt())
144+
val CLEAR_N = intArrayOf(0xff0b0f25.toInt(), 0xff252b42.toInt())
145+
// ////////////
146+
val OVERCAST_D = intArrayOf(0xff33425f.toInt(), 0xff617688.toInt())//0xff748798, 0xff617688
147+
val OVERCAST_N = intArrayOf(0xff262921.toInt(), 0xff23293e.toInt())//0xff1b2229, 0xff262921
148+
// ////////////
149+
val RAIN_D = intArrayOf(0xff4f80a0.toInt(), 0xff4d748e.toInt())
150+
val RAIN_N = intArrayOf(0xff0d0d15.toInt(), 0xff22242f.toInt())
151+
// ////////////
152+
val FOG_D = intArrayOf(0xff688597.toInt(), 0xff44515b.toInt())
153+
val FOG_N = intArrayOf(0xff2f3c47.toInt(), 0xff24313b.toInt())
154+
155+
// ////////////
156+
val SNOW_D = intArrayOf(0xff4f80a0.toInt(), 0xff4d748e.toInt())//临时用RAIN_D凑数的
157+
val SNOW_N = intArrayOf(0xff1e2029.toInt(), 0xff212630.toInt())
158+
// ////////////
159+
val CLOUDY_D = intArrayOf(0xff4f80a0.toInt(), 0xff4d748e.toInt())//临时用RAIN_D凑数的
160+
val CLOUDY_N = intArrayOf(0xff071527.toInt(), 0xff252b42.toInt())// 0xff193353 };//{ 0xff0e1623, 0xff222830 }
161+
// ////////////
162+
val HAZE_D = intArrayOf(0xff616e70.toInt(), 0xff474644.toInt())// 0xff999b95, 0xff818e90
163+
val HAZE_N = intArrayOf(0xff373634.toInt(), 0xff25221d.toInt())
164+
165+
// ////////////
166+
val SAND_D = intArrayOf(0xffb5a066.toInt(), 0xffd5c086.toInt())//0xffa59056
167+
val SAND_N = intArrayOf(0xff312820.toInt(), 0xff514840.toInt())
168+
}
127169
}

app/src/main/java/com/xiasuhuei321/gank_kotlin/datasource/local/SaveJson2DB.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteDatabase
88
import android.database.sqlite.SQLiteOpenHelper
99
import android.database.sqlite.SQLiteQuery
1010
import com.xiasuhuei321.gank_kotlin.context
11+
import com.xiasuhuei321.gank_kotlin.datasource.bean.GankData
1112
import com.xiasuhuei321.gank_kotlin.datasource.bean.Town
1213
import com.xiasuhuei321.gank_kotlin.extension.io_main
1314
import io.reactivex.Flowable
@@ -23,7 +24,7 @@ import kotlin.collections.ArrayList
2324
2425
*/
2526

26-
fun LocalDataSource.saveCity(dbName: String, jsonStr: String, observer: FlowableSubscriber<HashMap<String, List<Town>>>) {
27+
fun LocalDataSource<GankData>.saveCity(dbName: String, jsonStr: String, observer: FlowableSubscriber<HashMap<String, List<Town>>>) {
2728
val cityArray = JSONArray(jsonStr)
2829
val dbHelper = WeatherDBHelper(context, dbName, null, 1)
2930
val db = dbHelper.writableDatabase
@@ -42,7 +43,7 @@ fun LocalDataSource.saveCity(dbName: String, jsonStr: String, observer: Flowable
4243
if (map[cityName] == null) {
4344
// 说明还不存在
4445
dbHelper.insertIntoCity(cityName,
45-
it.optString(WeatherDBHelper.cityEN), db)
46+
it.optString(WeatherDBHelper.cityEN))
4647
townList = ArrayList<Town>()
4748
map.put(cityName, townList)
4849
}
@@ -53,7 +54,7 @@ fun LocalDataSource.saveCity(dbName: String, jsonStr: String, observer: Flowable
5354
dbHelper.insertIntoTown(cityName,
5455
town.townName,
5556
town.townID,
56-
town.townEn, db)
57+
town.townEn)
5758
townList.add(town)
5859
}
5960
j.onNext(map)
@@ -63,7 +64,7 @@ fun LocalDataSource.saveCity(dbName: String, jsonStr: String, observer: Flowable
6364
.subscribe(observer)
6465
}
6566

66-
fun LocalDataSource.readTownList(): HashMap<String, List<Town>> {
67+
fun LocalDataSource<GankData>.readTownList(): HashMap<String, List<Town>> {
6768
var map = HashMap<String, List<Town>>()
6869
return map
6970
}
@@ -88,20 +89,20 @@ class WeatherDBHelper(context: Context?, name: String?, factory: ((db: SQLiteDat
8889
db.execSQL(CREATE_TABLE_TOWER)
8990
}
9091

91-
fun insertIntoCity(city: String, cityen: String, db: SQLiteDatabase): Long {
92+
fun insertIntoCity(city: String, cityen: String): Long {
9293
val values = ContentValues()
9394
values.put(cityName, city)
9495
values.put(cityEN, cityen)
95-
return db.insert("city", null, values)
96+
return writableDatabase.insert("city", null, values)
9697
}
9798

98-
fun insertIntoTown(city: String, town: String, townId: String, townen: String, db: SQLiteDatabase): Long {
99+
fun insertIntoTown(city: String, town: String, townId: String, townen: String): Long {
99100
val values = ContentValues()
100101
values.put(townID, townId)
101102
values.put(townEN, townen)
102103
values.put(cityName, city)
103104
values.put(townName, town)
104-
return db.insert("town", null, values)
105+
return writableDatabase.insert("town", null, values)
105106
}
106107

107108
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {

app/src/main/res/layout/activity_main.xml

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
android:layout_height="match_parent"
55
android:visibility="visible">
66

7-
<FrameLayout
8-
android:id="@+id/containerFl"
7+
<com.xiasuhuei321.gank_kotlin.customview.weather.WeatherView
98
android:layout_width="match_parent"
10-
android:layout_height="match_parent">
11-
12-
</FrameLayout>
9+
android:layout_height="match_parent"
10+
android:id="@+id/weatherWv"/>
1311

1412

1513
<RelativeLayout
1614
android:id="@+id/rl_test"
1715
android:layout_width="match_parent"
1816
android:layout_height="match_parent"
19-
android:background="#6699cc"
2017
android:visibility="visible">
2118

2219
<LinearLayout

app/src/main/res/values/colors.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<color name="colorPrimary">#3F51B5</color>
44
<color name="colorPrimaryDark">#303F9F</color>
55
<color name="colorAccent">#FF4081</color>
6+
<color name="sky_blue">#6699cc</color>
67
</resources>

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext.kotlin_version = '1.1.50'
45
ext.kotlin_version = '1.1.2-4'
56
repositories {
67
apply from: "conf.gradle"

network/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

network/build.gradle

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
if (!isLibrary) {
2+
apply plugin: 'com.android.application'
3+
} else {
4+
apply plugin: 'com.android.library'
5+
}
6+
7+
8+
9+
android {
10+
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
11+
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
12+
defaultConfig {
13+
if (!isLibrary) {
14+
applicationId "com.xiasuhuei321.network"
15+
}
16+
minSdkVersion rootProject.ext.android["minSdkVersion"]
17+
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
18+
versionCode rootProject.ext.android["versionCode"]
19+
versionName rootProject.ext.android["versionName"]
20+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
21+
22+
}
23+
buildTypes {
24+
release {
25+
minifyEnabled false
26+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
27+
}
28+
}
29+
30+
sourceSets {
31+
main {
32+
if (isLibrary) {
33+
manifest.srcFile 'src/main/AndroidManifest.xml'
34+
} else {
35+
manifest.srcFile 'src/main/module/AndroidManifest.xml'
36+
}
37+
}
38+
}
39+
}
40+
41+
dependencies {
42+
compile fileTree(dir: 'libs', include: ['*.jar'])
43+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
44+
exclude group: 'com.android.support', module: 'support-annotations'
45+
})
46+
47+
compile 'com.android.support:appcompat-v7:26.+'
48+
testCompile 'junit:junit:4.12'
49+
compile 'com.android.support.constraint:constraint-layout:1.0.2'
50+
51+
compile rootProject.ext.dependencies["retrofit"]
52+
compile rootProject.ext.dependencies["rxjava2"]
53+
compile rootProject.ext.dependencies["rxandroid"]
54+
compile rootProject.ext.dependencies["gson"]
55+
compile rootProject.ext.dependencies["retrofit_rxjava"]
56+
compile rootProject.ext.dependencies["retrofit_gson"]
57+
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
58+
}
59+
apply plugin: 'kotlin-android'
60+
repositories {
61+
mavenCentral()
62+
}

network/proguard-rules.pro

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/luojun/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile

0 commit comments

Comments
 (0)