I have a listview with some elements, I have to make this list scroll automatically, with a slow scroll.
When he reaches the last element he stops.
If new items are added in the queue, then the listview must restart, until it reaches the last element again.
Any suggestions?
I suggest, thought your adapter implemented in effective way. so this code is just scrolls listview
you need to try another values of variables
final long totalScrollTime = Long.MAX_VALUE; //total scroll time. I think that 300 000 000 years is close enouth to infinity. if not enought you can restart timer in onFinish()
final int scrollPeriod = 20; // every 20 ms scoll will happened. smaller values for smoother
final int heightToScroll = 20; // will be scrolled to 20 px every time. smaller values for smoother scrolling
listView.post(new Runnable() {
#Override
public void run() {
new CountDownTimer(totalScrollTime, scrollPeriod ) {
public void onTick(long millisUntilFinished) {
listView.scrollBy(0, heightToScroll);
}
public void onFinish() {
//you can add code for restarting timer here
}
}.start();
}
});
use the below method to scroll your listview. for example:
scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
method:
private void scrollMyTableView(View v,final int var, int fromY, int toY, int delay){
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(v, "scrollY", fromY, toY).setDuration(2000);
objectAnimator.setStartDelay(delay);
objectAnimator.start();
objectAnimator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
}
and if new item added to adapter and need to again go to top of listview and again come back to bottom.you can use below code to do this:
myAdapter.registerDataSetObserver(new DataSetObserver() {
#Override
public void onChanged() {
super.onChanged();
activityListview.setSelection(0);
scrollMyTableView(activityListview,11,0,getTotalHeightofListView(activityListview),0);
}
});
To calculate the height of listview use below method
public int getTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
}
return totalHeight;
}
Use listview smoothScrollToPosition(int position) function.
Please have a look on this post also
https://www.codota.com/code/java/methods/android.widget.ListView/smoothScrollToPosition
Related
I want to set animation on ImageView when user put more than 3 characters. I wrote a code to do this. Main problem is when I keep putting more than 3 characters the animation starts again from 0 position always when new character is added, so it doesn't looks good.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_user_first);
initViews();
imageButtonNext.setEnabled(false);
horizontalScrollView.post(() -> horizontalScrollView.scrollTo(0, 0));
horizontalScrollView.setOnTouchListener((v, event) -> true);
editTextEnterName.addTextChangedListener(nameTextWatcher);
pulse = AnimationUtils.loadAnimation(NewUserFirst.this, R.anim.pulse);
imageButtonNext.setOnClickListener(view -> {
intent = new Intent(this, NewUserSecond.class);
startActivity(intent);
});
}
public void buttonAnimation(){
if(buttonBoolean){
imageButtonNext.setAnimation(pulse);
imageButtonNext.setColorFilter(ContextCompat.getColor(NewUserFirst.this, R.color.accent));
imageButtonNext.setEnabled(true);
} else {
imageButtonNext.clearAnimation();
imageButtonNext.setColorFilter(ContextCompat.getColor(NewUserFirst.this, R.color.gray));
imageButtonNext.setEnabled(false);
}
}
private final TextWatcher nameTextWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
nameInput = editTextEnterName.getText().toString().trim();
if (nameInput.length() >= 3) {
buttonBoolean = true;
} else {
buttonBoolean = false;
}
}
#Override
public void afterTextChanged(Editable editable) {
buttonAnimation();
}
};
So the question is, what changes I need to make in my code to set animation, and do not renew it every time when add next character.
Here is the answer:
public void buttonAnimation(){
if(buttonBoolean){
if (!imageButtonNext.isEnabled()) {
imageButtonNext.setAnimation(pulse);
imageButtonNext.setColorFilter(ContextCompat.getColor(this, R.color.accent));
imageButtonNext.setEnabled(true);
}
} else {
imageButtonNext.clearAnimation();
imageButtonNext.setColorFilter(ContextCompat.getColor(this, R.color.gray));
imageButtonNext.setEnabled(false);
}
}
We need to add if statement in animation method. Program check animation is currently enabled, when yes, program do nothing. Result is animation triggered once live his own life until we modify edittext to length less than 3.
I make some sort of quiz app and I want that every time an answer button get clicked there will be an animation changing to to yellow. after 0.25sec the next question will appear (and I want it to change back to the old color)
I already did it here:
public void onClick(View v) {
x
for (int i = 0; i < 4; i++) {
if(v == answerButtons[i]){
int j = i;
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(250);
valueAnimator.setEvaluator(new ArgbEvaluator());
valueAnimator.setIntValues(Color.WHITE,Color.YELLOW);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
answerButtons[j].setBackgroundColor((int)valueAnimator.getAnimatedValue());
}
});
valueAnimator.start();
((MainActivity)getActivity()).num++;
}
}
it does change the color but right after that the next answers(and question) support to appear on the same buttons.
I tried this:
for (int i = 0; i < 4; i++) {
if(v == answerButtons[i]){
int j = i;
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
answerButtons[j].setBackgroundColor(Color.rgb(63, 81, 181));
Toast.makeText(getActivity(), "Just changed color to: button "+(j+1), Toast.LENGTH_SHORT).show();
//display(questions.get(((MainActivity)getActivity()).num));
}
},250);
}
}
x
}
but it mostly doesn't work sometimes it does. any idea why it doesn't work everytime? (the only thing it doesn't do is to change the color)
I got a solution for this one :)
apparently the postDelay function is not that precise..
int j = i;
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(250);
valueAnimator.setEvaluator(new ArgbEvaluator());
valueAnimator.setIntValues(Color.WHITE,Color.YELLOW);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
answerButtons[j].setBackgroundColor((int)valueAnimator.getAnimatedValue());
}
});
valueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
answerButtons[j].setBackgroundColor(Color.parseColor("#3F51B5"));
display(questions.get(((MainActivity)getActivity()).num));
}
});
valueAnimator.start();
So this is my spin the bottle animation:
public static final Random RANDOM = new Random();
private int lastAngle = -1;
private ImageView bottle;
private void spinTheBottle() {
int angle = RANDOM.nextInt(3600 - 360) + 360;
float pivotX = bottle.getWidth() / 2;
float pivotY = bottle.getHeight() / 2;
final Animation animRotate = new RotateAnimation(lastAngle == -1 ? 0 : lastAngle, angle, pivotX, pivotY);
lastAngle = angle;
animRotate.setDuration(1500);
animRotate.setFillAfter(true);
bottle.startAnimation(animRotate);
}
How can I detect when the animation is done? I tried adding Animator.AnimatorListener, but that does not execute at all:
Animator.AnimatorListener listener = new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
Toast.makeText(FlasketutenActivity.this, "onAnimationStart", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationEnd(Animator animation) {
Toast.makeText(FlasketutenActivity.this, "onAnimationEnd", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationCancel(Animator animation) {
Toast.makeText(FlasketutenActivity.this, "onAnimationCancel", Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationRepeat(Animator animation) {
Toast.makeText(FlasketutenActivity.this, "onAnimationRepeat", Toast.LENGTH_SHORT).show();
}
};
Any tips?
It looks like you're creating the listener, but not assigning it to your animation. Try calling setAnimationListener on your animation object like this:
animRotate.setAnimationListener(new Animation.AnimationListener() {
//Listener methods
});
The first.Keep the ImageView witch and height above of zero at time
rotateAnimation.setDuration(1500);
rotateAnimation.setFillAfter(true);
rotateAnimation.setFillEnabled(true);
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.d(TAG, "onAnimationStart: ");
}
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "onAnimationEnd: ");
}
#Override
public void onAnimationRepeat(Animation animation) {
Log.d(TAG, "onAnimationRepeat: ");
}
});
view.startAnimation(rotateAnimation);
it is working in my project.
But I use the animation always in xmls,it is Easy to operate and relatively by code.
Animation and Animator is not a same things.
That's all.
Assign your listener that you have created to the animation
animRotate.setAnimationListener(listener);
I have implemented an Android listview with flip checkboxes (GMail style) as shown here: http://techiedreams.com/gmail-like-flip-animated-multi-selection-list-view-with-action-mode/
The problem I have (even in the source code example): If there are multiple items I click/check in a fast sequence, the previous animation stops and will be restared. In other words, the previous animation does not finish and is interrupted by the second one.
Simple download the source code and try it on your own. In comparisson to gmail all animations, no matter how fast I'm clicking, are performed from the start to the end.
I can't find the piece of code in the demo source code to change this behaviour.
Any hints?
Thanks in advance :)
This is the closest I could get to Gmail's flip animation:
In the getView() method (I'm using contextual action mode):
holder.check.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mActionMode == null) {
checked = new SparseBooleanArray(alllists.size());
mActionMode = ((Activity) getActivity()).startActionMode(mActionModeCallback);
animateRow(position);
checked.put(position, true);
mActionMode.setTitle("1");
mActionMode.getMenu().getItem(0).setVisible(true);
} else {
animateRow(position);
if (checked.get(position)) {
checked.put(position, false);
if (checked.indexOfValue(true) < 0) {
mActionMode.finish();
} else {
mActionMode.setTitle(String.valueOf(getCheckedCount()));
}
} else {
checked.put(position, true);
mActionMode.setTitle(String.valueOf(getCheckedCount()));
}
}
}
}
);
Then the animateRow() method:
private void animateRow(final int position) {
if (position >= listView.getFirstVisiblePosition() && position <= listView.getLastVisiblePosition()) {
Log.i("animateRow", String.valueOf(position));
ScaleAnimation anim = new ScaleAnimation(1, 0, 1, 1, Animation.RELATIVE_TO_SELF, 0.5f, 0, 0);
anim.setDuration(150);
anim.setRepeatCount(1);
anim.setRepeatMode(Animation.REVERSE);
viewa = listView.getChildAt(position - listView.getFirstVisiblePosition());
final Button chkButton = (Button) viewa.findViewById(R.id.logo);
if (checked.get(position)) {
anim.setAnimationListener(
new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.i("anim" + position, "Start");
}
#Override
public void onAnimationRepeat(Animation animation) {
Log.i("anim" + position, "Repeat" + animation.getRepeatCount());
viewa.setBackgroundResource(R.color.row_background);
chkButton.setBackgroundResource(R.color.button_background);
chkButton.setText(String.valueOf(alllists.get(position).itemsCount));
}
#Override
public void onAnimationEnd(Animation animation) {
Log.i("anim" + position, "END");
}
}
);
chkButton.startAnimation(anim);
} else {
anim.setAnimationListener(
new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.i("anim" + position, "Start");
}
#Override
public void onAnimationRepeat(Animation animation) {
viewa.setBackgroundResource(R.color.checked_row_background);
chkButton.setBackgroundResource(R.color.checked_button_background);
chkButton.setText("✓");
}
#Override
public void onAnimationEnd(Animation animation) {
Log.i("anim" + position, "END");
}
}
);
chkButton.startAnimation(anim);
}
}
}
I hope it helps.. :)
Now you can forget that method to flip a view, since I've developed a new library FlipView fully customizable, this is exactly what you want. You will be able to swap any kind of views and layouts with any kind of animation you desire.
Please have a look.
So I have code that adjusts System Brightness and I have properly implemented the seek bar but when I adjust the seekbar the Screen Brightness does not change. I looked into a few other posts and the solution seems to be to create a Dummy class that opens and closes really quickly. If I do that though then I feel like there would be a ton of lag because the screen would be refreshing constantly as the seekbar is moving.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ContentResolver cr = getContentResolver();
try{
int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);
Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(lp);
}
catch (Settings.SettingNotFoundException a)
{
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//Toast.makeText(MainActivity.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
Also another problem with the refresh is that I am running this brightness control in a screen where there is a camera preview and killing the camera each time the dummy screen loads would take some time and further increase the lag
I did see this post but I couldnt figure out how to implement it. Since I am not using preferences
I have a seekbar in my preferences to update screen brightness. Is there a way to refresh the UI to see the live change?
I solved the problem with the following code it now works as intended!
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
BackLightValue = (float)arg1/100;
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = BackLightValue;
getWindow().setAttributes(layoutParams);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
int SysBackLightValue = (int)(BackLightValue * 255);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
SysBackLightValue);
}
Use my following code it perfectly running :
in your whatever xml file :
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:max="10"
android:progressDrawable="#drawable/progress"
android:scaleType="fitCenter"
android:thumb="#drawable/thumb" />
note : take attributes as much you need in seekbar.
in your whatever activity use following method and call that :
private void setSeekbar()
{
seekBar = (SeekBar) findViewById(R.id.seekBar1);
seekBar.setMax(255);
float curBrightnessValue = 0;
try {
curBrightnessValue = android.provider.Settings.System.getInt(
getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
int screen_brightness = (int) curBrightnessValue;
seekBar.setProgress(screen_brightness);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue,
boolean fromUser) {
progress = progresValue;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS,
progress);
}
});
}