我一直在寻找这个问题的解决方案,但找不到。 我的应用程序包含一个带有文本视图的滚动视图。我想以这样的方式制作动画:当滚动时,必须突出显示滚动到中心的文本视图,并在滚动离开时逐渐返回到原始状态中央。我不知道如何让它发挥作用。我尝试了很多不同的方法但失败了。请帮我。 谢谢。
public class FifthActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ScrollView sv = (ScrollView)findViewById(R.id.scroll);
LinearLayout l1 = (LinearLayout)findViewById(R.id.linear_layout);
final TextView tv1 = (TextView)findViewById(R.id.nernst_eqation);
final TextView tv2 = (TextView)findViewById(R.id.hardnessofwater);
final TextView tv3 = (TextView)findViewById(R.id.Cao_in_cement_sample);
final TextView tv4 = (TextView)findViewById(R.id.COD);
final TextView tv5 = (TextView)findViewById(R.id.copper_in_brass);
final TextView tv6 = (TextView)findViewById(R.id.alkalinity);
final TextView tv7 = (TextView)findViewById(R.id.Iron);
final TextView tv8 = (TextView)findViewById(R.id.cu_colourimetry);
final TextView tv9 = (TextView)findViewById(R.id.potentiometry);
final TextView tv10 = (TextView)findViewById(R.id.conductometry);
final TextView tv11 = (TextView)findViewById(R.id.pka);
final TextView tv12 = (TextView)findViewById(R.id.flame);
final TextView tv13 = (TextView)findViewById(R.id.Viscosity);
int sWidth= this.getResources().getDisplayMetrics().widthPixels;
int sHeight= this.getResources().getDisplayMetrics().heightPixels;
final int CenterX=sWidth/2;
final int CenterY = sHeight/2;
sv.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if ((tv1.getTop() + tv1.getHeight() / 2) == CenterY && (tv1.getLeft()+ tv1.getWidth()/2) == CenterX) {
tv1.setBackgroundResource(border);
}
else if ((tv2.getTop() + tv2.getHeight() / 2) == CenterY && (tv2.getLeft()+tv2.getWidth()/2) == CenterX) {
tv2.setBackgroundResource(border);
}
else if ((tv3.getTop() + tv3.getHeight() / 2) == CenterY && (tv3.getLeft()+tv3.getWidth()/2 ) == CenterX) {
tv3.setBackgroundResource(border);
}
else if ((tv4.getTop() + tv4.getHeight() / 2) == CenterY && (tv4.getLeft()+tv4.getWidth()/2) == CenterX) {
tv4.setBackgroundResource(border);
}
else if ((tv5.getTop() + tv5.getHeight() / 2) == CenterY && (tv5.getLeft()+tv5.getWidth()/2) == CenterX) {
tv5.setBackgroundResource(border);
}
else if ((tv6.getTop() + tv6.getHeight() / 2) == CenterY && (tv6.getLeft()+tv6.getWidth()/2) == CenterX) {
tv6.setBackgroundResource(border);
}
else if ((tv7.getTop() + tv7.getHeight() / 2) == CenterY && (tv7.getLeft()+tv7.getWidth()/2) == CenterX) {
tv7.setBackgroundResource(border);
}
else if ((tv8.getTop() + tv8.getHeight() / 2) == CenterY && (tv8.getLeft()+tv8.getWidth()/2) == CenterX) {
tv8.setBackgroundResource(border);
}
else if ((tv9.getTop() + tv9.getHeight() / 2) == CenterY && (tv9.getLeft()+tv9.getWidth()/2) == CenterX ){
tv9.setBackgroundResource(border);
}
else if ((tv10.getTop() + tv10.getHeight() / 2) == CenterY && (tv10.getLeft()+tv10.getWidth()/2) == CenterX) {
tv10.setBackgroundResource(border);
}
else if ((tv11.getTop() + tv11.getHeight() / 2) == CenterY && (tv11.getLeft()+tv11.getWidth()/2) == CenterX) {
tv11.setBackgroundResource(border);
}
else if ((tv12.getTop() + tv12.getHeight() / 2) == CenterY && (tv12.getLeft()+tv12.getWidth()/2) == CenterX) {
tv12.setBackgroundResource(border);
}
else if ((tv13.getTop() + tv13.getHeight() / 2) == CenterY && (tv13.getLeft()+tv13.getWidth()/2) == CenterX) {
tv13.setBackgroundResource(border);
}
}
});
}