zhuyihenzheng / 0118Test

0 stars 0 forks source link

textview闪动 imageontouch #2

Open zhuyihenzheng opened 6 years ago

zhuyihenzheng commented 6 years ago

package com.example.zhengying.imagestest;

import android.graphics.PorterDuff; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView;

import com.romainpiel.shimmer.Shimmer; import com.romainpiel.shimmer.ShimmerTextView;

public class MainActivity extends AppCompatActivity {

private int charGaps;
private int startPosition;
private int endPosition;

ShimmerTextView tv;
Shimmer shimmer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (ShimmerTextView) findViewById(R.id.txtView);
    toggleAnimation(tv);

    ImageView v = (ImageView) findViewById(R.id.image1);
    v.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    ImageView view = (ImageView) v;
                    //overlay is black with transparency of 0x77 (119)
                    view.getDrawable().setColorFilter(0x88000000, PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    //clear the overlay
                    view.getDrawable().clearColorFilter();
                    view.invalidate();
                    break;
                }
            }

            return false;
        }
    });
}

public void toggleAnimation(View target) {
    if (shimmer != null && shimmer.isAnimating()) {
        shimmer.cancel();
    } else {
        shimmer = new Shimmer();
        shimmer.start(tv);
    }
}

// class MyThread extends Thread { // //used for stopping thread // boolean flag; // // //init flag to true so that method run continuously // public MyThread() { // flag = true; // } // // //set flag false, if want to stop this thread // public void setFlag(boolean flag) { // this.flag = flag; // } // // @Override // public void run() { // super.run(); // while (flag) { // try { // Thread.sleep(intervalMiliSeconds); // runOnUiThread(new Runnable() { // @Override // public void run() { // // Spannable spn = new SpannableString(txtView // .getText().toString()); // spn.setSpan(new ForegroundColorSpan(Color.WHITE), // startPosition, endPosition, // Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // txtView.setText(spn); // // startPosition++; // endPosition++; // endPosition %= (lengthOfString + charGaps); // startPosition %= lengthOfString; // // if (startPosition == 0) { // endPosition = charGaps; // startPosition = 0; // } // // if (endPosition > lengthOfString) { // endPosition = lengthOfString; // } // // Log.d("Home", "Start : " + startPosition + " End : " + endPosition); // // } // }); // } catch (Exception e) { // e.printStackTrace(); // } // } // } // } }

zhuyihenzheng commented 6 years ago

package com.example.zhengying.imagestest;

import android.graphics.Color; import android.graphics.PorterDuff; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ForegroundColorSpan; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.TextView;

import com.romainpiel.shimmer.Shimmer;

public class MainActivity extends AppCompatActivity {

private int charGaps;
private int startPosition;
private int endPosition;
private int lengthOfString;

TextView txtView;
Shimmer shimmer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtView = (TextView) findViewById(R.id.txtView);

    TextView txtView = (TextView) findViewById(R.id.txtView);

    charGaps = 3;

    startPosition = 0;
    endPosition = charGaps;

    lengthOfString = txtView.getText().length();

    MyThread thread = new MyThread();

    thread.start();

    ImageView v = (ImageView) findViewById(R.id.image1);
    v.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    ImageView view = (ImageView) v;
                    //overlay is black with transparency of 0x77 (119)
                    view.getDrawable().setColorFilter(0x88000000, PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    //clear the overlay
                    view.getDrawable().clearColorFilter();
                    view.invalidate();
                    break;
                }
            }

            return false;
        }
    });
}

class MyThread extends Thread {
    //used for stopping thread
    boolean flag;

    //init flag to true so that method run continuously
    public MyThread() {
        flag = true;
    }

    //set flag false, if want to stop this thread
    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        super.run();
        while (flag) {
            try {
                Thread.sleep(100);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Spannable spn = new SpannableString(txtView
                                .getText().toString());
                        spn.setSpan(new ForegroundColorSpan(Color.BLUE),
                                startPosition, endPosition,
                                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        txtView.setText(spn);

                        startPosition++;
                        endPosition++;
                        endPosition %= (lengthOfString + charGaps);
                        startPosition %= lengthOfString;

                        if (startPosition == 0) {
                            endPosition = charGaps;
                            startPosition = 0;
                        }

                        if (endPosition > lengthOfString) {
                            endPosition = lengthOfString;
                        }

                        Log.d("Home", "Start : " + startPosition + " End : " + endPosition);

                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

}

zhuyihenzheng commented 6 years ago

Color.argb(0xff, 0x00, 0x00, 0x00)

zhuyihenzheng commented 6 years ago

https://stackoverflow.com/questions/17574330/adding-different-types-of-items-a-list-view

zhuyihenzheng commented 6 years ago

public static void setListViewHeightBasedOnChild(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();

    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        if (listItem instanceof ViewGroup) {
            listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}

}

zhuyihenzheng commented 6 years ago

https://stackoverflow.com/questions/4817900/android-fragments-and-animation