今天学习了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. 动画--android图片点击放大动画,并遮挡旁边的控件

    http://blog.csdn.net/s13488941815/article/details/40649823: 首先是点击放大可以使用android自带的缩放动画,因为要遮盖其他控件,就需要控 ...

  2. 管道过滤器模式(Pipe and Filter)与组合模式(修改)

    转自:http://haolloyin.blog.51cto.com/1177454/348277 之前在 benjielin 前辈的博客中看到“管道过滤器(Pipe-And-Filter)模式(ht ...

  3. UVA 11853 [dfs乱搞]

    /* 大连热身E题 不要低头,不要放弃,不要气馁,不要慌张 题意: 在1000×1000的格子内有很多个炮弹中心,半径给定. 为某人能否从西部边界出发,从东部边界走出. 不能输出不能,能的话输出最北边 ...

  4. python 日期相关的各种操作总结

    用 Python 做项目时,经常会遇到与日期转换相关,日期计算相关的功能,动不动就要去查python手册,感觉麻烦,因此把自己常用的一些东西,总结了一下,总体说来到目前为止遇到如下一些需求: 1. 用 ...

  5. 修改.gitignore后让其生效

    在使用git的时候我们有时候需要忽略一些文件或者文件夹.我们一般在仓库的根目录创建.gitignore文件在提交之前,修改.gitignore文件,添加需要忽略的文件.然后再做add commit p ...

  6. 给iOS开发新手送点福利,简述文本属性Attributes的用法

    给iOS开发新手送点福利,简述文本属性Attributes的用法   文本属性Attributes 1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSF ...

  7. 规则引擎集成接口(七)规则引擎调用Java类

    规则引擎调用Java类 通过myEclipse编写一个简单工程,其中方法是两数相加等到结果,既结果1=输入值1+输入值2.实现规则调用外部接口的方法有三种. 1:接口实例:在myEclipse中制作一 ...

  8. AngularJS-系统代码的配置和翻译

    前言:在Web开发中常常会遇到这样的情况,有些页面的下拉选项是固定不变的几个,比如:性别,一般有男.女.保密等.对于这样的情形我们一般在数据库中存储的是数字或者其对应的代码,如果是可维护的需要系统给出 ...

  9. 代码管理——如何连接Git Server,下载代码

    最近一个项目需要与国外团队合作,而他们的代码在GitLab上,需要使用Git工具连接服务器,对于我这样一个SVN的拥护者,当然很高兴去接受这个工作了(鄙视一下目前单位还使用ClearCase). 但操 ...

  10. Orcle基本语句(三)

    COMMIT; --查询表内所有内容 SELECT * FROM stu_info; --查询部分列,并赋予别名 SELECT stu_id 学生标号,stu_name 学生姓名 FROM stu_i ...