This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
app/src/main/java/com/hippo/nimingban/widget/MarqueeReplyView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2016 Hippo Seven | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.hippo.nimingban.widget; | ||
|
||
/* | ||
* Created by Hippo on 12/5/2016. | ||
*/ | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.util.AttributeSet; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.TextSwitcher; | ||
import android.widget.ViewSwitcher; | ||
|
||
import com.hippo.nimingban.R; | ||
import com.hippo.nimingban.client.data.Reply; | ||
import com.hippo.yorozuya.MathUtils; | ||
|
||
public class MarqueeReplyView extends TextSwitcher { | ||
|
||
private Reply[] mReplies; | ||
private int mIndex; | ||
private boolean mCanUpdate; | ||
private boolean mShouldUpdate; | ||
|
||
private final Runnable mInvalidateTask = new Runnable() { | ||
@Override | ||
public void run() { | ||
mShouldUpdate = true; | ||
invalidate(); | ||
} | ||
}; | ||
|
||
private final ViewSwitcher.ViewFactory mReplyViewFactory = new ViewSwitcher.ViewFactory() { | ||
@Override | ||
public View makeView() { | ||
return LayoutInflater.from(getContext()).inflate( | ||
R.layout.item_list_reply, MarqueeReplyView.this, false); | ||
} | ||
}; | ||
|
||
public MarqueeReplyView(Context context) { | ||
super(context); | ||
init(); | ||
} | ||
|
||
public MarqueeReplyView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
init(); | ||
} | ||
|
||
private void init() { | ||
setWillNotDraw(false); | ||
setFactory(mReplyViewFactory); | ||
} | ||
|
||
private boolean canMarquee() { | ||
return mReplies != null && mReplies.length > 0; | ||
} | ||
|
||
private long getMarqueeInterval() { | ||
return MathUtils.random(3000, 5001); | ||
} | ||
|
||
private void nextMarquee() { | ||
stopMarquee(); | ||
if (canMarquee()) { | ||
mCanUpdate = true; | ||
postDelayed(mInvalidateTask, getMarqueeInterval()); | ||
} | ||
} | ||
|
||
private void stopMarquee() { | ||
mCanUpdate = false; | ||
removeCallbacks(mInvalidateTask); | ||
} | ||
|
||
public void setReplies(Reply[] replies) { | ||
mReplies = replies; | ||
mIndex = 0; | ||
mCanUpdate = false; | ||
mShouldUpdate = false; | ||
if (replies != null && replies.length > 0) { | ||
setCurrentText(replies[0].getNMBDisplayContent()); | ||
} else { | ||
setCurrentText(null); | ||
} | ||
nextMarquee(); | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
super.onDraw(canvas); | ||
|
||
if (mCanUpdate && mShouldUpdate && canMarquee()) { | ||
mIndex = (mIndex + 1) % mReplies.length; | ||
setText(mReplies[mIndex].getNMBDisplayContent()); | ||
nextMarquee(); | ||
} | ||
mShouldUpdate = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters