今天学习了AsyncTask Android 的异步机制。我简单的实现我的一个小小案例——qq记步数。然后穿插一个画圆形图片的知识点。

由于所学知识有限,目前我计数,还有排名等等我就简单的利用随机数实现。多有不是之处见谅啊。

我们的xml layout布局文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.qqsport.MainActivity" >
<!-- 头部 -->
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="...heyhhz...." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="45dp">
<!-- 附近排名 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <TextView
android:id="@+id/scort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="附近排名" /> <TextView
android:id="@+id/fujin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="第0名" />
</LinearLayout>
<!-- 头像 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/face"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/face"/>
<TextView
android:id="@+id/bushu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</LinearLayout>
<!-- 排行榜 -->
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list"
android:text="排行榜"/>
<TextView
android:id="@+id/place"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第0名"
android:layout_marginTop="30dp"/>
</LinearLayout>
</LinearLayout> </LinearLayout>

我们的MainActivity.class 文件  其中我们头像变成圆形的代码也在其中。

package com.example.qqsport;

/**
* 1.写一个随机数充当 该用户的步数
* 2.实现 从1加到 该随机数的效果
*
*/ import java.util.Random; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends Activity {
private static final String TAG = "RoundImage";
private ImageView mImg;
private TextView tv_bushu,tv_place,tv_fujin;
private Random r = new Random();
private MyTask myTask = new MyTask(this); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
huatu();
int bushu = r.nextInt(5000)+5000;
//随机设置排名
int place = r.nextInt(50); //设置随机数 附近排名
int fujin = r.nextInt(200); //初始化步数
tv_bushu = (TextView) findViewById(R.id.bushu); //初始化 排行榜控件
tv_place = (TextView) findViewById(R.id.place);
//附近
tv_fujin = (TextView) findViewById(R.id.fujin);
myTask.execute(tv_bushu,bushu,tv_place,place,tv_fujin,fujin); } //画圆形图片
private void huatu() {
//初始化控件
mImg = (ImageView) findViewById(R.id.face);
//裁剪图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.face, options);
Log.d(TAG, "original outwidth:"+options.outWidth);
//此宽度是目标 imageView 希望的大小,你可以自定义imageView 然后获得ImageView 的宽度
int dstWidth = 100;
//我们需要加载的图片可能很大,我们先对原有的图片进行裁剪
int sampleSize = calculateInSampleSize(options, dstWidth, dstWidth);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Log.d(TAG, "sample size:" + sampleSize);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.face, options);
//绘制图片
Bitmap resultBmp = Bitmap.createBitmap(dstWidth, dstWidth, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
Canvas canvas = new Canvas(resultBmp);
//画图
canvas.drawCircle(dstWidth / 2, dstWidth / 2, dstWidth / 2, paint);
//选择交集去上层图片
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bmp, new Rect(0, 0, bmp.getWidth(), bmp.getWidth()), new Rect(0, 0, dstWidth, dstWidth), paint);
mImg.setImageBitmap(resultBmp);
bmp.recycle();
} private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if( height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && ( halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
} } return inSampleSize;
} }

接下来是我们后台加载,异步机制。前面也说了,小编在这是利用随机数进行的,然后在这里加载出来的数据由于是同时传入的,他按照先后顺序进行显示,并没有实现同步的一个操作。这里要大家自己去实现下啦。

 package com.example.qqsport;

 import android.app.Activity;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast; public class MyTask extends AsyncTask{ private Activity runActivit; private TextView tv_bs,tv_fj,tv_pla;
private int bs,pla,fj;
public MyTask (Activity activity){
this.runActivit = activity;
} @Override
protected Object doInBackground(Object... params) {
//我的步数
tv_bs = (TextView) params[0];
bs = (Integer) params[1];
for(int i = 1; i <= bs; i++) { publishProgress(i,1);
}
//排名
tv_pla = (TextView) params[2];
pla = (Integer) params[3];
for (int i = 1; i <= pla; i++){
publishProgress(i,2); }
//附近排名
tv_fj = (TextView) params[4];
fj = (Integer) params[5];
for (int i = 1; i <= fj; i++){
publishProgress(i,3); }
return "加载完成";
} //onPostExecute 后台数据结束后调用的方法
@Override
protected void onPostExecute(Object result) {
if( bs > 7000) {
Toast.makeText(runActivit, "哎哟,不错哟今天", 1).show();
}else {
Toast.makeText(runActivit, "偶尔放慢脚步可以思考人生", 1).show();
} } //onProgressUpdate 当前面使用了publishProgress 这个方法的时候就调用。
@Override
protected void onProgressUpdate(Object... values) {
Integer aa = (Integer) values[0];
Integer bb = (Integer) values[1];
if(bb == 1){
tv_bs.setText(aa + "");
}
if(bb == 2){ tv_pla.setText("第"+aa + "名");
}
if(bb == 3){ tv_fj.setText("第"+aa + "名");
} } }

大家可以拷贝代码自己试下。[微笑]欢迎大家交流指教,我也是初学者。

Android_AsyncTaskDemo之QQ记步数(画圆形图片知识)的更多相关文章

  1. 在android中画圆形图片的几种办法

    在开发中常常会有一些需求,比方显示头像,显示一些特殊的需求,将图片显示成圆角或者圆形或者其它的一些形状. 可是往往我们手上的图片或者从server获取到的图片都是方形的.这时候就须要我们自己进行处理, ...

  2. android绘制圆形图片的两种方式

    看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap ...

  3. iOS常见用户头像的圆形图片裁剪常见的几种方法

    在开发中,基本上APP的用户头像的处理都需要把用户所上传的方形图片,处理为圆形图片.在这里就总结三种常见的处理圆形图片的方法. 1.使用位图上下文 2.使用UIView的layer进行处理 3.使用r ...

  4. Android实现圆形图片

     情景再现: 写Android程序也有一段时间了,今天突然被问怎么实现一个圆形图片,很多app图像是圆形的.但是用户上传的图像可不是圆的,所以问题就来了,需要我们代码实现圆形图片.但是大脑飞转想到第三 ...

  5. UIImage类扩展返回一个带边框的圆形图片

    /** * 将image转换为圆型带边框的图片(最好写一个UIImage的类扩展) * * @param name 图片的名字 * @param borderWidth 外层边框的宽度 * @para ...

  6. Android圆形图片--ImageView

    [ RoundImageView.java ] package com.dxd.roundimageview; import android.content.Context; import andro ...

  7. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  8. 使用CAShapeLayer来实现圆形图片加载动画[译]

    原文链接 : How To Implement A Circular Image Loader Animation with CAShapeLayer 原文作者 : Rounak Jain 译文出自 ...

  9. Android圆形图片自定义控件

    Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content ...

随机推荐

  1. Mysql的视图、存储过程、函数、索引全解析

    视图是查询命令结果构成的一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集合,并可以当作表来查询使用. 1创建视图 --格式:C ...

  2. MVC 访问IFrame页面Session过期后跳转到登录页面

    Web端开发时,用户登录后往往会通过Session来保存用户信息,Session存放在服务器,当用户长时间不操作的时候,我们会希望服务器保存的Session过期,这个时候,因为Session中的用户信 ...

  3. dbms_output.put_line 不显示

    再写oracle sql时候,写循环语句,想知道循环对不对,使用dbms_output.put_line()没有打印出任何东西,网上查找发现少了一句. 加上 set serverouput on  就 ...

  4. SQL SERVER 导出数据,数据与结构,结构

    1.右键数据库->任务->生成脚本 2.选择数据库对象,可以整个表,也可以选择部分表 3.下一步,设置脚本编写选项.选择高级,在高级中,倒数第二项,'要编写脚本的数据的类型'中,可以选择导 ...

  5. javascript属性标签

  6. Nopcommerce 二次开发0

    Nopcommerce  是国外的一个高质量的开源b2c网站系统,基于EntityFramework6.0和MVC5.0,使用Razor模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的 ...

  7. Fragment笔记整理

    前言 一直在用Fragment,但是没有系统的整理过,Google了一下相关文章,看到了几篇,将几篇还不错的文章重点整理了下,很多是直接Copy的,只为做个笔记,以后翻来看比较方便,建议大家看一下下面 ...

  8. 1、B2BUA

    链接1:proxy和B2BUA的区别和联系:http://www.cnblogs.com/gnuhpc/archive/2012/12/11/2813499.html 链接2:http://blog. ...

  9. tr:even 与tr:odd

    :even匹配所有索引值为偶数的元素,从 0 开始计数查找表格的1.3.5...行(即索引值0.2.4...)<table> <tr><td>Header 1< ...

  10. oracle 查询执行过的SQL语句

    SELECT * FROM v$sqlarea t WHERE t.FIRST_LOAD_TIME between '2016-12-23/16:03:00' and '2016-12-23/16:0 ...