Skip to content

Commit

Permalink
update the TranslationX2Animate
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogero0o committed Jan 11, 2016
1 parent fcbd700 commit 538cedd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 20 deletions.
48 changes: 48 additions & 0 deletions Demo/src/main/java/com/roger/psdloadingview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import com.roger.psdloadingview.library.PsdLoadingView;
import com.roger.psdloadingview.library.animate.IAnimate;
import com.roger.psdloadingview.library.animate.TranslationX2Animate;
import com.roger.psdloadingview.library.animate.TranslationXAnimate;

public class MainActivity extends AppCompatActivity {

Expand All @@ -14,6 +21,47 @@ public class MainActivity extends AppCompatActivity {
final PsdLoadingView psd = (PsdLoadingView) findViewById(
R.id.psdloadingview);

Spinner spinner = (Spinner) findViewById(R.id.spinner);
// 建立数据源
String[] mItems = getResources().getStringArray(R.array.animate);
// 建立Adapter并且绑定数据源
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mItems);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

String[] languages = getResources().getStringArray(
R.array.animate);
Toast.makeText(MainActivity.this,
"你点击的是:" + languages[pos], Toast.LENGTH_SHORT)
.show();
IAnimate iAnimate;
switch (pos) {
case 0:
iAnimate = new TranslationXAnimate();
break;
case 1:
iAnimate = new TranslationX2Animate();
break;
default:
iAnimate = new TranslationXAnimate();
}
psd.init(iAnimate);
}


@Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});

findViewById(R.id.button).setOnClickListener(
new View.OnClickListener() {
@Override public void onClick(View v) {
Expand Down
18 changes: 9 additions & 9 deletions Demo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">

<EditText
Expand All @@ -27,7 +28,14 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Spinner
android:layout_marginTop="20dp"
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Spinner>

<Button
android:layout_marginTop="20dp"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:text="登陆"
Expand All @@ -37,17 +45,9 @@
<Button
android:id="@+id/success"
android:layout_gravity="center_horizontal"
android:text="成功"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/fail"
android:layout_gravity="center_horizontal"
android:text="失败"
android:text="结束"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@
*/
public class PsdLoadingView extends EditText {

private IAnimate mIAnimate;
private IAnimate mIAnimate = new TranslationX2Animate();


public PsdLoadingView(Context context) {
super(context);
init();
init(mIAnimate);
}


public PsdLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
init(mIAnimate);
}


public PsdLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
init(mIAnimate);
}


private void init() {
mIAnimate = new TranslationX2Animate();
mIAnimate.init(this);
public void init(IAnimate mIAnimate) {
this.mIAnimate = mIAnimate;
this.mIAnimate.init(this);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,53 @@
public class TranslationX2Animate extends BaseAnimate {

ArrayList<Float> movexArray;
boolean isBegining;
boolean isEnding;


@Override public void init(PsdLoadingView mPsdLoadingView) {
super.init(mPsdLoadingView);
}


@Override public void startLoading() {
super.startLoading();
isBegining = true;
isEnding = false;
}


@Override public void stopLoading() {
super.stopLoading();
movexArray.clear();
isBegining = false;
isEnding = true;
}


@Override public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!isStop) {
if (progress > 0.999f) {
isBegining = false;
}
if (movexArray == null || movexArray.size() < textLength) {
movexArray = new ArrayList<Float>(textLength);
for (int i = 0; i < textLength; i++) {
movexArray.add(0.0f);
}
}
for (int i = 0; i < textLength; i++) {
if ((float) i < progress * (float) textLength &&
progress * (float) textLength < ((float) i + 1f)) {
movexArray.set(i, progress * (mPsdLoadingView.getWidth() -
(textLength + 2) * distance));
if (isEnding || isBegining ||
((float) i < progress * (float) textLength &&
progress * (float) textLength <
((float) i + 1f))) {
float temp = progress * (mPsdLoadingView.getWidth() -
(textLength + 2) * distance);
float maxtemp = ((i + 1) / (float) textLength) *
(mPsdLoadingView.getWidth() -
(textLength + 2) * distance);
movexArray.set(i, Math.min(temp, maxtemp));
}
canvas.drawText(DOT + "", 0, 1,
movexArray.get(i) + (i + 1) * distance, startY, mPaint);
Expand Down
7 changes: 7 additions & 0 deletions Library/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="animate">
<item>TranslationX</item>
<item>TranslationX2</item>
</string-array>
</resources>

0 comments on commit 538cedd

Please sign in to comment.