① android網路載入進度條怎麼使用
由於Android的界面更新只能通過自己的UI線程進行操作,所以我們需要用到Handler在進行更新界面的操作。
1、聲明變數
private Handler handler = new Handler();
private ProgressDialog progressDialog = null;
2、在開始進行網路連接時顯示進度條對話框
progressDialog = ProgressDialog.show(MyActivity.this, "請稍等...", "獲取數據中...", true);
3、在handler.post中進行界面更新
public void setListAdapter(){
handler.post(new Runnable() {
public void run() {
//將獲取到的數據更新到列表中
MyListView.setAdapter(saImageItems);
}
}
});
4、開個新的線程進行網路連接獲取數據
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
//向伺服器請求數據
mapList=MyAPI.getAllDatas();
setListAdapter(mapList);
//更新完列表數據,則關閉對話框
progressDialog.dismiss();
}}).start();
整個過程就這樣完成了,下面是效果圖:
② Android自定義View實現可拖拽的進度條
在onSizeChanged方法中進行計算,這時可以得到一條與控制項寬度相同的直線,並把路徑設置給PathMeasure
使用PathMeasure得出當前進度的路徑並進行繪制,這里我將上一步的繪制放在了一起
這個矩形的寬度需要我們用繪制最長的文字來確定其寬高
另外矩形的顯示位置也是以當前進度所在的點為中心點
文字顯示的位置計算起來就比較簡單了,直接用上一步算出的矩形的中心點即可,不過這里需要調整文字繪制的垂直的偏移,這樣才能實現文字垂直居中
實現拖拽需要對onTouchEvent方法進行處理,也就是當手指觸摸矩形區域的時候,根據手指橫向滑動的偏移來設置當前的進度,具體如下
為了適配高度的wrap_content屬性,我們需要計算出控制項最小需要顯示的高度
這里我們是用顯示進度的矩形的高度作為控制項最小的高度的,因為矩形的高度是所有圖形最高的一個
https://gitee.com/itfitness/seek-progress-bar
③ 怎樣實現在android實現帶進度條的上傳效果
實現在android實現帶進度條的上傳效果效果如圖:用到以下兩個類就可實現帶進度條的文件上傳:
1、CustomMultiPartEntity extends MultipartEntity,
2、HttpMultipartPost extends AsyncTask
代碼如下:
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
public class CustomMultipartEntity extends MultipartEntity {
private final ProgressListener listener;
public CustomMultipartEntity(final ProgressListener listener) {
super();
this.listener = listener;
}
public CustomMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener) {
super(mode);
this.listener = listener;
}
public CustomMultipartEntity(HttpMultipartMode mode, final String boundary,
final Charset charset, final ProgressListener listener) {
super(mode, boundary, charset);
this.listener = listener;
}
@Override
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new CountingOutputStream(outstream, this.listener));
}
public static interface ProgressListener {
void transferred(long num);
}
public static class CountingOutputStream extends FilterOutputStream {
private final ProgressListener listener;
private long transferred;
public CountingOutputStream(final OutputStream out, final ProgressListener listener) {
super(out);
this.listener = listener;
this.transferred = 0;
}
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
this.transferred += len;
this.listener.transferred(this.transferred);
}
public void write(int b) throws IOException {
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}
}
該類計算寫入的位元組數,我們需要在實現ProgressListener中的trasnfered()方法,更行進度條
public class HttpMultipartPost extends AsyncTask<HttpResponse, Integer, TypeUploadImage> {
ProgressDialogpd;
longtotalSize;
@Override
protectedvoidonPreExecute(){
pd= newProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Uploading Picture...");
pd.setCancelable(false);
pd.show();
}
@Override
(HttpResponse... arg0) {
HttpClienthttpClient = newDefaultHttpClient();
HttpContexthttpContext = newBasicHttpContext();
HttpPosthttpPost = newHttpPost("http://herpderp.com/UploadImage.php");
try{
= newCustomMultipartEntity(
newProgressListener() {
@Override
public void transferred(longnum){
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
// We use FileBody to transfer an image
multipartContent.addPart("uploaded_file", newFileBody(
newFile(m_userSelectedImagePath)));
totalSize= multipartContent.getContentLength();
// Send it
httpPost.setEntity(multipartContent);
HttpResponseresponse = httpClient.execute(httpPost, httpContext);
String serverResponse = EntityUtils.toString(response.getEntity());
ResponseFactoryrp = newResponseFactory(serverResponse);
return(TypeImage) rp.getData();
}
catch(Exception e) {
System.out.println(e);
}
returnnull;
}
@Override
protectedvoidonProgressUpdate(Integer... progress){
pd.setProgress((int) (progress[0]));
}
@Override
protectedvoidonPostExecute(TypeUploadImageui) {
pd.dismiss();
}
}
在 transferred()函數中調用publishProgress((int) ((num / (float) totalSize) * 100));
在onProgressUpdate()實現上傳進度的更新操作
④ android 圓形進度條 中間怎麼寫 demo
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<corners android:radius="20dp" />
<gradient
android:angle="0"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="20dp" />
<gradient
android:angle="0"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="20dp" />
<gradient
android:angle="0"
android:endColor="#8000ff00"
android:startColor="#80ff0000" />
</shape>
</clip>
</item>
<!--
下面是標準的綠色進度條
<item
android:id="@android:id/background"
android:drawable="@drawable/security_progress_bg">
</item>
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/security_progress">
</item>
<item
android:id="@android:id/progress"
android:drawable="@drawable/security_progress">
</item>
-->
</layer-list>
⑤ Android開發怎麼自定義繪制如下圖中這種進度條急需!在線等!
一)變換前背景
先來看看progressbar的屬性:
1. <ProgressBar
2. android:id="@+id/progressBar"
3. style="?android:attr/progressBarStyleHorizontal"
4. android:layout_width="match_parent"
5. android:layout_height="wrap_content"
6. android:layout_margin="5dip"
7. android:layout_toRightOf="@+id/progressBarV"
8. android:indeterminate="false"
9. android:padding="2dip"
10. android:progress="50" />
根據style="?android:attr/progressBarStyleHorizontal",我們找到源碼中的style.xml
1. <style name="Widget.ProgressBar.Horizontal">
2. <item name="android:indeterminateOnly">false</item>
3. <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
4. <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
5. <item name="android:minHeight">20dip</item>
6. <item name="android:maxHeight">20dip</item>
7. </style>
看到
<item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
木有,繼續發掘源碼,找到drawable下面的progress_horizontal.xml,這就是我們今天的主角了:
1. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
2.
3. <item android:id="@android:id/background">
4. <shape>
5. <corners android:radius="5dip" />
6. <gradient
7. android:startColor="#ff9d9e9d"
8. android:centerColor="#ff5a5d5a"
9. android:centerY="0.75"
10. android:endColor="#ff747674"
11. android:angle="270"
12. />
13. </shape>
14. </item>
15.
16. <item android:id="@android:id/secondaryProgress">
17. <clip>
18. <shape>
19. <corners android:radius="5dip" />
20. <gradient
21. android:startColor="#80ffd300"
22. android:centerColor="#80ffb600"
23. android:centerY="0.75"
24. android:endColor="#a0ffcb00"
25. android:angle="270"
26. />
27. </shape>
28. </clip>
29. </item>
30.
31. <item android:id="@android:id/progress">
32. <clip>
33. <shape>
34. <corners android:radius="5dip" />
35. <gradient
36. android:startColor="#ffffd300"
37. android:centerColor="#ffffb600"
38. android:centerY="0.75"
39. android:endColor="#ffffcb00"
40. android:angle="270"
41. />
42. </shape>
43. </clip>
44. </item>
45.
46. </layer-list>
看到android:id="@android:id/progress"木有,看到android:id="@android:id/secondaryProgress"木有
把這個文件復制到自己工程下的drawable,就可以隨心所欲的修改shape的屬性,漸變,圓角等等
那麼怎麼放一個圖片進去呢,ok,新建progress_horizontal1.xml:
1. <?xml version="1.0" encoding="utf-8"?>
2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3.
4. <item android:id="@android:id/progress" android:drawable="@drawable/progressbar" />
5.
6. </layer-list>
在android:drawable中指定你處理好的圖片
然後回到布局中
1. <ProgressBar
2. android:id="@+id/progressBar1"
3. android:layout_width="match_parent"
4. android:layout_height="wrap_content"
5. android:layout_below="@+id/progressBar"
6. android:layout_margin="5dip"
7. android:layout_toRightOf="@+id/progressBarV"
8. android:background="@drawable/progress_bg"
9. android:indeterminate="false"
10. android:indeterminateOnly="false"
11. android:maxHeight="20dip"
12. android:minHeight="20dip"
13. android:padding="2dip"
14. android:progress="50"
15. android:progressDrawable="@drawable/progress_horizontal1" />
android:background="@drawable/progress_bg"指定背景
android:progressDrawable="@drawable/progress_horizontal1"前景使用上面的progress_horizontal1
要是還不行
你來我們群里說吧
這里是開發者互相學習交流的 有大神
讓他們給你解釋你的疑問 號 碼look at my n a m e.....
⑥ 跪求一個 android 視頻緩存完整demo(帶進度條)
在做手機開發時,經常碰到一些比較耗時的操作,這個時候進度條就開始派上用場了。 這個demo展示了ProgressBar進度條和ProgressDialog進度框。 一、ProgressDialog進度框,效果如圖所示: 代碼如下: 復制代碼 代碼如下: //進度對話框按鈕監聽 class ProssButtonListener implements OnClickListener { @Override public void onClick(View v) { myDialog = ProgressDialog.show(ProgressbarDemo.this, "進度條標題", "進度條內容", true); new Thread() { public void run() { try { /* 在這里寫上要背景運行的程序片段 */ /* 為了明顯看見效果,以暫停3秒作為示範 */ sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { // 卸載所創建的myDialog對象。 myDialog.dismiss(); } } }.start(); /* 開始運行運行線程 */ } } 二、進度條對話框,這里用兩種情況來動態顯示進度條刻度 1、handle方法 效果圖如下: 代碼如下: 復制代碼 代碼如下: //進度條handle按鈕監聽 class ProssBarHandleButtonListener implements OnClickListener { @Override public void onClick(View v) { progressBarHandle.setVisibility(View.VISIBLE); myProssBarhandleText.setVisibility(View.VISIBLE); progressBarHandle.setMax(1000); new Thread() { public void run() { for(int i=0;i<=1000;){ try { /* 在這里寫上運行的進度條 */ Message msg = new Message(); msg.what = 1; msg.getData().putInt("size", i); handler.sendMessage(msg);//handle發送消息 /* 為了明顯看見效果,以暫停1秒作為示範 */ sleep(100); i+=10; } catch (Exception e) { handler.obtainMessage(-1).sendToTarget(); e.printStackTrace(); } } } }.start(); /* 開始運行運行線程 */ } } //handle接收消息 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: progressBarHandle.setProgress(msg.getData().getInt("size")); float num = (float)progressBarHandle.getProgress()/(float)progressBarHandle.getMax(); int result = (int)(num*100); System.out.println("progressBarHandle.getProgress()======="+progressBarHandle.getProgress()); myProssBarhandleText.setText(result+ "%"); if(progressBarHandle.getProgress()==progressBarHandle.getMax()){ Toast.makeText(ProgressbarDemo.this, "下載成功", 1).show(); progressBarHandle.setVisibility(View.GONE); myProssBarhandleText.setVisibility(View.GONE); } break; case -1: Toast.makeText(ProgressbarDemo.this,"下載失敗", 1).show(); break; } } }; 2、使用AsyncTask方法,效果圖與handle效果一樣 具體代碼如下: 復制代碼 代碼如下: //進度條synctask按鈕監聽 class ProssBarSyncButtonListener implements OnClickListener { @Override public void onClick(View v) { new AsyncLoader().execute((Void)null); } } //AsyncTask 任務執行 class AsyncLoader extends AsyncTask<Void, Integer, Integer> { @Override // 在doInBackground方法執行前執行 protected void onPreExecute(){ progressBarHandle.setVisibility(View.VISIBLE); myProssBarhandleText.setVisibility(View.VISIBLE); progressBarHandle.setMax(100000); } // 做具體的耗時操作 protected Integer doInBackground(Void... params) { //這里以1萬的進度條刻度來顯示 int totalSize = 100000; for (int i = 0; i < 100000; ) { publishProgress(i); //通過推送消息傳遞數據給onProgressUpdate方法執行 i+=10; } return totalSize; } // 在doInBackground方法執行過程中執行 protected void onProgressUpdate(Integer... progress) { progressBarHandle.setProgress(progress[0]); float num = (float)progressBarHandle.getProgress()/(float)progressBarHandle.getMax(); int result = (int)(num*100); myProssBarhandleText.setText(result+ "%"); } // 在doInBackground方法結束後執行 protected void onPostExecute(Integer result) { Toast.makeText(ProgressbarDemo.this, "下載成功,下載了"+result, 1).show(); myProssBarhandleText.setVisibility(View.GONE); progressBarHandle.setVisibility(View.GONE); } } 點擊下載DEMO示例
⑦ android中如何獲取listview中其中一個Item中的進度條對象來更新下載進度
public class MainActivity extends Activity
{
private ListView listView;
private DownloadAdapter adapter;
private List<String>行純 lists = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 10; i++)
{
lists.add("察仔button" + i);
}
listView = (ListView) findViewById(R.id.listView1);
adapter = new DownloadAdapter(this, lists);
listView.setAdapter(adapter);
}
}
public class DownloadAdapter extends BaseAdapter
{
private Context context;
private List<String> list;
private LayoutInflater inflater;
public DownloadAdapter(Context context, List<String> list)
{
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position)
{
//敗帶汪 TODO Auto-generated method stub
return list.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
MyHolder myHolder = null;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.item, null);
myHolder = new MyHolder();
myHolder.bar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
myHolder.button = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(myHolder);
}
else
{
myHolder = (MyHolder) convertView.getTag();
}
myHolder.button.setText(list.get(position));
MyDownLoadThread down = new MyDownLoadThread(myHolder.bar,
myHolder.button);
myHolder.button.setOnClickListener(down);
return convertView;
}
class MyHolder
{
private ProgressBar bar;
private Button button;
}
public class MyDownLoadThread extends Thread implements OnClickListener
{
private ProgressBar pBar;
private Button btn;
private int index = 0;
private boolean isStart = false;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
pBar.setProgress(index);
}
};
public MyDownLoadThread(ProgressBar pBar, Button btn)
{
this.pBar = pBar;
this.btn = btn;
}
@Override
public void onClick(View v)
{
if (!isStart)
{
isStart = true;
btn.setText("開始下載");
pBar.setMax(100);
this.start();
}
}
@Override
public void run()
{
while (index <= 100)
{
index++;
handler.sendEmptyMessage(0);
try
{
sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (index > 100)
{
btn.setText("下載完成");
break;
}
}
}
}
}
簡單的demo 僅供參考,希望對你有用
⑧ android 繪制進度條
看起來代碼挺長,其實都是在獲取自定義屬性,沒什麼技術含量。
寬度不變,所以的自定義屬性不涉及寬度,高度呢,只考慮不是EXACTLY的情況(用戶明確指定了,就不管了),根據padding和進度條寬度算出自己想要的,如果非EXACTLY下,進行exceptHeight封裝,傳入給控制項進行測量高度。
橫向的滾動條繪制肯定需要一些屬性,比如已/未到達進度的顏色、寬度,文本的顏色、大小等。
本來呢,我是想通過系統ProgressBar的progressDrawable,從裡面提取一些屬性完成繪制需要的參數的。但是,最終呢,反而讓代碼變得復雜。所以最終還是改用自定義屬性。 說道自定義屬性,大家應該已經不陌生了。
1、
1、自定義屬性
values/attr_progress_bar.xml:
[html] view plain
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="">
<attr name="progress_unreached_color" format="color" />
<attr name="progress_reached_color" format="color" />
<attr name="progress_reached_bar_height" format="dimension" />
<attr name="progress_unreached_bar_height" format="dimension" />
<attr name="progress_text_size" format="dimension" />
<attr name="progress_text_color" format="color" />
<attr name="progress_text_offset" format="dimension" />
<attr name="progress_text_visibility" format="enum">
<enum name="visible" value="0" />
<enum name="invisible" value="1" />
</attr>
</declare-styleable>
<declare-styleable name="RoundProgressBarWidthNumber">
<attr name="radius" format="dimension" />
</declare-styleable>
</resources>
2、構造中獲取
[java] view plain
public class extends ProgressBar
{
private static final int DEFAULT_TEXT_SIZE = 10;
private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1;
private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da;
private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2;
private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2;
private static final int DEFAULT_SIZE_TEXT_OFFSET = 10;
/**
* painter of all drawing things
*/
protected Paint mPaint = new Paint();
/**
* color of progress number
*/
protected int mTextColor = DEFAULT_TEXT_COLOR;
/**
* size of text (sp)
*/
protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
/**
* offset of draw progress
*/
protected int mTextOffset = dp2px(DEFAULT_SIZE_TEXT_OFFSET);
/**
* height of reached progress bar
*/
protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR);
/**
* color of reached bar
*/
protected int mReachedBarColor = DEFAULT_TEXT_COLOR;
/**
* color of unreached bar
*/
protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR;
/**
* height of unreached progress bar
*/
protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR);
/**
* view width except padding
*/
protected int mRealWidth;
protected boolean mIfDrawText = true;
protected static final int VISIBLE = 0;
public (Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public (Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
setHorizontalScrollBarEnabled(true);
obtainStyledAttributes(attrs);
mPaint.setTextSize(mTextSize);
mPaint.setColor(mTextColor);
}
/**
* get the styled attributes
*
* @param attrs
*/
private void obtainStyledAttributes(AttributeSet attrs)
{
// init values from custom attributes
final TypedArray attributes = getContext().obtainStyledAttributes(
attrs, R.styleable.);
mTextColor = attributes
.getColor(
R.styleable._progress_text_color,
DEFAULT_TEXT_COLOR);
mTextSize = (int) attributes.getDimension(
R.styleable._progress_text_size,
mTextSize);
mReachedBarColor = attributes
.getColor(
R.styleable._progress_reached_color,
mTextColor);
mUnReachedBarColor = attributes
.getColor(
R.styleable._progress_unreached_color,
DEFAULT_COLOR_UNREACHED_COLOR);
mReachedProgressBarHeight = (int) attributes
.getDimension(
R.styleable._progress_reached_bar_height,
mReachedProgressBarHeight);
mUnReachedProgressBarHeight = (int) attributes
.getDimension(
R.styleable._progress_unreached_bar_height,
mUnReachedProgressBarHeight);
mTextOffset = (int) attributes
.getDimension(
R.styleable._progress_text_offset,
mTextOffset);
int textVisible = attributes
.getInt(R.styleable._progress_text_visibility,
VISIBLE);
if (textVisible != VISIBLE)
{
mIfDrawText = false;
}
attributes.recycle();
}
3、onMeasure
剛才不是出onDraw裡面寫寫就行了么,為什麼要改onMeasure呢,主要是因為我們所有的屬性比如進度條寬度讓用戶自定義了,所以我們的測量也得稍微變下。
[java] view plain
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec)
{
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY)
{
float textHeight = (mPaint.descent() + mPaint.ascent());
int exceptHeight = (int) (getPaddingTop() + getPaddingBottom() + Math
.max(Math.max(mReachedProgressBarHeight,
mUnReachedProgressBarHeight), Math.abs(textHeight)));
heightMeasureSpec = MeasureSpec.makeMeasureSpec(exceptHeight,
MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
測量完,就到我們的onDraw了~~~
⑨ android 的ExpandableListView下面怎麼樣放置進度條
1. 先弄一個TextView作為指示器放在ListView的上面,跟列表上緣平齊。
2. 處理列表的上下滾動回調:
Java代碼 if(第一行是group){ if(第二行也是group) 隱藏指示器; </SPAN></li> else if(第二行是第啟高一行展開的第一個child) 顯示指示器; </SPAN></li>} else { </SPAN></li> if(第二行是group 且第一行顯示不完整時) 隱藏指示器; </SPAN></li>} if(第一行是group){ if(第二行也是group) 隱藏指示器; else if(第二行是第一行展開的第一個child) 顯示指示器; } else { if(第二行是group 且第一行顯示不完整時) 隱藏指示器; } 就這樣了,是吧?Java代碼 public class GroupHeaderIndicator extends TextView implements OnScrollListener { </SPAN></li> @Override </SPAN></li> public void onScroll(AbsListView view, int firstVisibleItem, </SPAN></li> int visibleItemCount, int totalItemCount) { </SPAN></li> // TODO Auto-generated method stub </SPAN></li> ExpandableListView listView = (ExpandableListView)view; //當前第一行歸屬的組ID </SPAN></li> int gid = ExpandableListView.getPackedPositionGroup(listView.getExpandableListPosition(firstVisibleItem)); </SPAN></li> //當前第一行的子ID </SPAN></li> int cid = ExpandableListView.getPackedPositionChild(listView.getExpandableListPosition(firstVisibleItem)); </SPAN></li> //當前第二行的子ID </SPAN></li> int nid = ExpandableListView.getPackedPositionChild(listView.getExpandableListPosition(firstVisibleItem+1)); </SPAN></li> BuddiesListAdapter adapter=(BuddiesListAdapter)listView.getExpandableListAdapter(); String gtitle=(gid>-1)?adapter.getGroup(gid).toString():""; </SPAN></li> //如果第一行和第跡滲二行都是組header,不需要顯示 </SPAN></li> if(cid==-1 && nid==-1){ </SPAN></li> this.setVisibility(View.GONE); </SPAN></li> } else { </SPAN></li> //當前真的是一個child,而且下一行是group </SPAN></li> if(nid==-1 && cid>=0){ </SPAN></li>悄州尺 this.setVisibility(View.GONE); </SPAN></li> return; </SPAN></li> } this.setVisibility(View.VISIBLE); </SPAN></li> this.setText(gtitle); </SPAN></li> this.postInvalidate(); </SPAN></li> } } } public class GroupHeaderIndicator extends TextView implements OnScrollListener { @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub ExpandableListView listView = (ExpandableListView)view; //當前第一行歸屬的組ID int gid = ExpandableListView.getPackedPositionGroup(listView.getExpandableListPosition(firstVisibleItem)); //當前第一行的子ID int cid = ExpandableListView.getPackedPositionChild(listView.getExpandableListPosition(firstVisibleItem)); //當前第二行的子ID int nid = ExpandableListView.getPackedPositionChild(listView.getExpandableListPosition(firstVisibleItem+1)); BuddiesListAdapter adapter=(BuddiesListAdapter)listView.getExpandableListAdapter(); String gtitle=(gid>-1)?adapter.getGroup(gid).toString():""; //如果第一行和第二行都是組header,不需要顯示 if(cid==-1 && nid==-1){ this.setVisibility(View.GONE); } else { //當前真的是一個child,而且下一行是group if(nid==-1 && cid>=0){ this.setVisibility(View.GONE); return; } this.setVisibility(View.VISIBLE); this.setText(gtitle); this.postInvalidate(); } } } 一切就這么簡單,然後看看它放的位置是:Xhtml代碼 <FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android"</SPAN></li> android:layout_width="fill_parent" </SPAN></li> android:layout_height="fill_parent" </SPAN></li> android:padding="8.0dip" </SPAN></li> android:layout_weight="1.0"> </SPAN></li> <ExpandableListView </SPAN></li> android:id="@id/buddies_list" </SPAN></li> android:scrollbars="vertical" </SPAN></li> android:layout_width="fill_parent" </SPAN></li> android:layout_height="fill_parent" </SPAN></li> android:layout_marginLeft="0.0dip" </SPAN></li> android:drawSelectorOnTop="false" </SPAN></li> android:scrollingCache="true" </SPAN></li> android:layout_weight="1.0" </SPAN></li> android:fastScrollEnabled="false" </SPAN></li> android:footerDividersEnabled="true" </SPAN></li> android:cacheColorHint="#00000000" </SPAN></li> android:groupIndicator="@drawable/expander_group" </SPAN></li> style="@style/Widget.ListViewGreen" mce_style="@style/Widget.ListViewGreen" </SPAN></li> /> </SPAN></li> <cn.sharetop.demo.ui.GroupHeaderIndicator </SPAN></li> android:id="@id/buddies_group_indicator" </SPAN></li> android:textColor="#333333" </SPAN></li> android:gravity="left|center" </SPAN></li> android:paddingLeft="32.0dip" </SPAN></li> android:layout_width="fill_parent" </SPAN></li> android:layout_height="44.0dip" </SPAN></li> android:background="@drawable/expand_group_bar" </SPAN></li> /> </SPAN></li> </FrameLayout> </SPAN></li><FrameLayout xmlns:android=" http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8.0dip" android:layout_weight="1.0"> <ExpandableListView android:id="@id/buddies_list" android:scrollbars="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="0.0dip" android:drawSelectorOnTop="false" android:scrollingCache="true" android:layout_weight="1.0" android:fastScrollEnabled="false" android:footerDividersEnabled="true" android:cacheColorHint="#00000000" android:groupIndicator="@drawable/expander_group" style="@style/Widget.ListViewGreen" mce_style="@style/Widget.ListViewGreen" /> <cn.sharetop.demo.ui.GroupHeaderIndicator android:id="@id/buddies_group_indicator" android:textColor="#333333" android:gravity="left|center" android:paddingLeft="32.0dip" android:layout_width="fill_parent" android:layout_height="44.0dip" android:background="@drawable/expand_group_bar" /> </FrameLayout>我為了省事,直接從TextView中派生出這個指示器,其實你可以更復雜點,從一個ViewGroup之類的東西來做出更多的效果。細心的朋友可以發現了,在QQ中,當上移列表時,有一個效果是下一個group header將指示器給推出屏幕的,圖省事,我沒這樣做,留給兄弟你去擴展了。此外,反編譯QQ的代碼可知,其實它的實現是自己做了一個叫 IphoneTreeView的類,繼承了ExpandableListView,這樣做更靈活也更好,同樣,如果要實現更完美的效果,還有很多事情要做的。
⑩ android 怎麼自定義繪制如下圖中這種進度條
下面是安卓學習手冊中實現各種進度條的截圖:
要想看各種進度條的實現代碼和文檔,直接去360手機助手中下載安卓學習手冊,例子文檔隨便看。
1、說明
在某些操作的進度中的可視指示器,為用戶呈現操作的進度,還它有一個次要的進度條,用來顯示中間進度,如在流媒體播放的緩沖區的進度。一個進度條也可不確定其進度。在不確定模式下,進度條顯示循環動畫。這種模式常用於應用程序使用任務的長度是未知的。
2、XML重要屬性
android:progressBarStyle:默認進度條樣式
android:progressBarStyleHorizontal:水平樣式
3 重要方法
getMax():返回這個進度條的范圍的上限
getProgress():返回進度
getSecondaryProgress():返回次要進度
incrementProgressBy(int diff):指定增加的進度
isIndeterminate():指示進度條是否在不確定模式下
setIndeterminate(boolean indeterminate):設置不確定模式下
setVisibility(int v):設置該進度條是否可視
4 重要事件
onSizeChanged(int w, int h, int oldw, int oldh):當進度值改變時引發此事件
5進度條的樣式
Widget.ProgressBar.Horizontal長形進度
Androidxml 布局:
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal "
/>
源碼:
private ProgressBar mProgress;
private int mProgressStatus=0;
private Handler mHandler=newHandler();
@Override
protected void onCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgress=(ProgressBar)findViewById(R.id.progress_bar);
new Thread(new Runnable(){
@Override
public void run(){
while(mProgressStatus<100){
mProgressStatus=doWork();
mHandler.post(new Runnable(){
@Override
public void run(){
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
}
效果圖:
帶第二進度的進度條
xml配置如下:
<ProgressBar
android:id="@+id/progress_bar_with_second"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="40"
android:secondaryProgress="70"
android:paddingTop="20dp"
android:paddingBottom="20dp"/>
這里我們設置了初始的進度為40,android:progress的值在mini和max之間即mini<=progressvalue<=max
設置了第二進度條的進度值為70,該值也在mini和max之間。
效果如下:
不確定模式進度條
xml配置文件:
<ProgressBar
android:id="@+id/progress_bar_indeterminate"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:indeterminateBehavior="cycle"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:progress="40" />
這里通過android:indeterminate="true"設置了當前為無模式進度條
效果如圖:
普通圓形進度:Widget.ProgressBar.Inverse
<ProgressBar
android:id="@+id/progress_bar1"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="50"
android:background="#ff00ff"
android:paddingTop="4dp" />
通過android:backgroup設置了背景色