我希望在长时间点击视图时启动动画。
代码如下。
public class Example_Dialog extends Dialog implements AnimationListener{
TextView view_Description1 = null;
View view_Button1 = null;
MessageCreateTask task;
public Example_Dialog( Context context , int theme ){
super( context , theme );
setContentView(R.layout.infomation_dialog);
view_Button1 = findViewById(R.id.common_dialog_button_1);
view_Description1 = (TextView)findViewById(R.id.common_dialog_description_1);
view_Description1.setHeight(0);
view_Button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
task = new MessageCreateTask(view_Description1);
task.execute();
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
class MessageCreateTask extends AsyncTask<Void, Void, String> {
TextView textView;
String messageOutput;
int totalLineNum;
public MessageCreateTask(TextView textView) {
this.textView = textView;
messageOutput = "";
totalLineNum = 0;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... params) {
for( int i= 0 ; i< 10 ; i++ ){
messageOutput += "a" + "<br>";
totalLineNum++;
}
return messageOutput;
}
@Override
protected void onPostExecute(String message) {
int dialogWidth = 838;
int dialogHeight = 26*totalLineNum;
textView.setWidth( dialogWidth );
textView.setText(Html.fromHtml(message));
HeightAnimation hanime_open = new HeightAnimation(textView , 0 , dialogHeight );
hanime_open = new HeightAnimation(textView , 0 , dialogHeight );
hanime_open.setDuration(300);
textView.startAnimation(hanime_open);
}
}
}
public class HeightAnimation extends Animation {
int targetHeight;
int startHeight;
TextView textView;
public HeightAnimation(TextView textView,int startHeight, int targetHeight) {
this.textView = textView;
this.targetHeight = targetHeight;
this.startHeight = startHeight;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newHeight = (int)(startHeight + (targetHeight - startHeight)*interpolatedTime);
textView.setHeight(newHeight);
if( (startHeight != 0) && (newHeight == 0) ){
textView.setVisibility(View.GONE);
}
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, ((View)textView.getParent()).getWidth(), parentHeight);
}
}
动画不是用上面的代码启动的,但它是通过用onlongclick替换onclick来启动的,如下所示。
view_Button1.setOnLongClickListener(new View.OnLongClickListener(){
public boolean onLongClick(View v){
task = new MessageCreateTask(view_Description1);
task.execute();
return true;
}
});
我想知道在onclick的情况下动画无法启动的原因。
答案 0 :(得分:0)
它不与onClickListener()
合作的原因是,view
没有焦点。因此,您需要按两次按钮,或者必须使用onlongClickListener()
。要首次制作view
clickable
,请执行以下操作:
在触摸模式下启用焦点,在XML布局中作为属性:
android:focusableInTouchMode="true"
或初始化按钮后以编程方式:
view_button1.setFocusableInTouchMode(true);
然后您可以将焦点设置为视图:
view_button1.requestFocus();