有没有可能在按钮中显示旋转进度条? 例如作为背景可绘制?按钮中的Android进度条
回答
是的。
您可以创建一个AnimationDrawable,如here所述,然后在您的按钮的XML中使用drawableLeft
标记(例如)。像这样:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/your_background_drawable_resource"
android:drawableLeft="@drawable/your_animation_drawable_resource"
android:text="@string/your_text_res">
</Button>
Thx!这对我来说很好! – Fabian 2011-01-28 18:55:58
我做了类似下面的样本代码.. 我希望我的代码帮助你:)
[main.xml中]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<Button
android:id="@+id/wheel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/icon_spin_animation"
/>
</LinearLayout>
[icon_spin_animation.xml]
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/wheel_db_update01" android:duration="50"/>
<item android:drawable="@drawable/wheel_db_update02" android:duration="50"/>
<item android:drawable="@drawable/wheel_db_update03" android:duration="50"/>
<item android:drawable="@drawable/wheel_db_update04" android:duration="50"/>
<item android:drawable="@drawable/wheel_db_update05" android:duration="50"/>
<item android:drawable="@drawable/wheel_db_update06" android:duration="50"/>
</animation-list>
[活动代码]
public class ProgressOnTheButtonActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
AnimationDrawable mFrameAnimation = null;
boolean mbUpdating = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnWheel = (Button)findViewById(R.id.wheel_button);
btnWheel.setOnClickListener(this);
mFrameAnimation = (AnimationDrawable) btnWheel.getBackground();
}
public void onClick(View v) {
if(v.getId() == R.id.wheel_button) {
if(!mbUpdating) {
mbUpdating = true;
new AsyncTaskForUpdateDB().execute("");
}
}
}
private class AsyncTaskForUpdateDB extends AsyncTask<String, Integer, ResultOfAsyncTask> {
@Override
protected void onPreExecute() {
mFrameAnimation.start();
super.onPreExecute();
}
@Override
protected ResultOfAsyncTask doInBackground(String... strData) {
ResultOfAsyncTask result = new ResultOfAsyncTask();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(ResultOfAsyncTask result) {
mFrameAnimation.stop();
mbUpdating = false;
}
@Override
protected void onCancelled() {
mFrameAnimation.stop();
mbUpdating = false;
super.onCancelled();
}
@Override
protected void onProgressUpdate(Integer... progress) {
}
}
private class ResultOfAsyncTask {
int iErrorCode = 0;
}
}
另一种选择是使用漂亮Spezi-Views,它包含了ProgressButton这是很容易使用:
<de.halfreal.spezi.views.ProgressButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me"
app:selectedText="I am loaded"
app:unselectedText="Press me again"
app:loadingDrawable="@drawable/spinner"
/>
和代码:
...
//show a rotation spinner, and no text (or the loading text)
progressButton.enableLoadingState();
//show no animation, but the selected/ unselected text
progressButton.disableLoadingState();
...
感谢这个信息....这是简单易用:) – Devrath 2014-04-04 10:33:31
是...只是环绕这两个按钮和progressBar里面的相对布局,像这样...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="right"
android:layout_alignTop="@+id/btnConnect"
android:layout_alignRight="@+id/btnConnect"
android:layout_alignEnd="@+id/btnConnect" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect"
android:id="@+id/btnConnect"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:width="200dp"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
<TextView
android:id="@+id/txtConnectStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnConnect"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Status : Not connected"
android:textSize="12dp"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="vertical"
想后的样本图像,但我没有足够的声誉,只是还没有...;)
为了Animatable
可绘制工作,你需要扩展Button
类并调用Animatable.start()
为可绘制。我做了一个实现此:
package com.example.yourapplication;
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
public class AnimateCompoundDrawableButton extends AppCompatButton {
public AnimateCompoundDrawableButton(Context context) {
super(context);
}
public AnimateCompoundDrawableButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AnimateCompoundDrawableButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
super.setCompoundDrawables(left, top, right, bottom);
startIfAnimatable(left);
startIfAnimatable(top);
startIfAnimatable(right);
startIfAnimatable(bottom);
}
@Override
public void setCompoundDrawablesRelative(Drawable start, Drawable top, Drawable end, Drawable bottom) {
super.setCompoundDrawablesRelative(start, top, end, bottom);
startIfAnimatable(start);
startIfAnimatable(top);
startIfAnimatable(end);
startIfAnimatable(bottom);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
// Makes a copy of the array, however we cannot do this otherwise.
for (Drawable drawable : getCompoundDrawables()) {
startIfAnimatable(drawable);
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// Makes a copy, however we cannot do this otherwise.
for (Drawable drawable : getCompoundDrawables()) {
stopIfAnimatable(drawable);
}
}
private void startIfAnimatable(Drawable drawable) {
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
private void stopIfAnimatable(Drawable drawable) {
if (drawable instanceof Animatable) {
((Animatable) drawable).stop();
}
}
}
我有同样的问题,所以我创建了这个专门的按钮:LoadingProgressButton
包括像这样的按钮:
<br.com.simplepass.loading_button_lib.CircularProgressButton
android:id="@+id/btn_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/circular_border_shape"
app:spinning_bar_width="4dp" <!-- Optional -->
app:spinning_bar_color="#FFF" <!-- Optional -->
app:spinning_bar_padding="6dp" <!-- Optional -->
而且使用这样的:
CircularProgressButton btn = (CircularProgressButton) findViewById(R.id.btn_id)
btn.startAnimation();
[do some async task. When it finishes]
//You can choose the color and the image after the loading is finished
btn.doneLoagingAnimation(fillColor, bitmap);
[or just revert de animation]
btn.revertAnimation();
- 1. android中的进度条onclick按钮?
- 2. 按钮上的进度条
- 3. 按钮点击进度条
- 4. Swift - 进度条按钮
- 5. HTML5中的进度条按钮
- 6. 没有Windows按钮的进度条
- 7. 点击按钮上的进度条
- 8. 进度条不按钮更新点击
- 9. 按钮点击减少进度条?
- 10. TabHost中的进度条android
- 11. Android中的进度条
- 12. android中的进度条
- 13. Android中的进度条
- 14. Android .....进度条
- 15. 进度条的Android
- 16. android-进度条延迟加载,当点击按钮时
- 17. 进度条加载活动或加载按钮进度
- 18. 在JavaFx中禁用进度条中的取消按钮
- 19. Android DialogFragment进度条
- 20. Windows Forms的进度按钮
- 21. Android的进度条样式
- 22. 饼图进度条的Android
- 23. 在android listview中保存状态进度条状态和按钮点击事件
- 24. 如何制作进度条显示上的按钮的顶部的Android
- 25. 进度在按钮的中间
- 26. Android中按钮的条件检查
- 27. 在Android中加载Imageview的进度条
- 28. Android中没有边框的进度条
- 29. android中的垂直进度条
- 30. 更新android中的进度条
我创建了我自己的类,它看起来像一个按钮更简单,更干净,而且不需要创建异步过程 – ademar111190 2011-12-19 22:15:34
我还有另一种解决方案:只需创建一个Framelayout并将Textview和一个进度条它。然后将onClickListener设置为FrameLayout。这将为您创建布局时提供更多灵活性:) – Fabian 2011-08-23 11:20:52