当前位置:  编程技术>移动开发
本页文章导读:
    ▪Athrun框架自定义控件的施用        Athrun框架自定义控件的使用  Athrun框架自定义控件的使用   无论Android还是iOS都支持自定义控件,而原始Athrun框架只支持系统控件,所以在使用Athrun框架进行功能自动化测试时需要根据项目.........
    ▪ 对View DrawingCache的了解        对View DrawingCache的理解 View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有   void setDrawingCacheEnabled(boolean flag), Bitmap getDrawingCache(boolean autoScale), void buildDrawingCache(boolean autoScale), void.........
    ▪ NSFileManager 资料应用       NSFileManager 文件应用 - (void)clearDisk { dispatch_async(self.ioQueue, ^ { [[NSFileManager defaultManager] removeItemAtPath:self.diskCachePath error:nil]; [[NSFileManager defaultManager] createDirectoryAtPath:self.diskCac.........

[1]Athrun框架自定义控件的施用
    来源: 互联网  发布时间: 2014-02-18
Athrun框架自定义控件的使用

 Athrun框架自定义控件的使用

 

无论Android还是iOS都支持自定义控件,而原始Athrun框架只支持系统控件,所以在使用Athrun框架进行功能自动化测试时需要根据项目的实际情况,自行扩展Athrun框架并添加项目中自定义控件的映射。如以下示例所示。

 

1 带有自定义控件的小应用:TestCustom。

 

  其中

CustomTextView.java

 

 package com.example.testcustom;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView{

 public CustomTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  setText("Custom");
  Drawable draw = getResources().getDrawable(R.drawable.huangqinbg);
  this.setCompoundDrawablesWithIntrinsicBounds(draw, draw, draw, draw);
  setBackgroundResource(R.drawable.huangqinbg);
 }

 public CustomTextView(Context context) {
  super(context);
  setText("Custom");
  Drawable draw = getResources().getDrawable(R.drawable.huangqinbg);
  this.setCompoundDrawablesWithIntrinsicBounds(draw, draw, draw, draw);
  setBackgroundResource(R.drawable.huangqinbg);
 }
}

 

DrawGraphics.java

 package com.example.testcustom;

import android.graphics.Canvas;

public interface DrawGraphics {
 public void  draw(Canvas canvas);

GameView.java

 package com.example.testcustom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class GameView extends View implements Runnable {
 private Paint mPaint = null;
 private DrawGraphics drawGraphics = null;
 private static int mCircleColor = Color.GREEN;
 private static int mLineColor = Color.GREEN;
 private static int mRectColor = Color.GREEN;

 public GameView(Context context) {

  super(context);
  mPaint = new Paint();
  new Thread(this).start();
 }
 
 public GameView(Context context, AttributeSet attrs) {
  super(context, attrs);
  mPaint = new Paint();
  new Thread(this).start();
 }
 
 public void onDraw(Canvas canvas) {
  super.onDraw(canvas);

  // 设置画布为黑色背景
  // canvas.drawColor(Color.BLACK);

  // 消除锯齿
  mPaint.setAntiAlias(true);

  // 设置图形为空心
  mPaint.setStyle(Paint.Style.STROKE);

  // 绘制空心几何图形
  drawGraphics = new DrawCircle();
  drawGraphics.draw(canvas);
  drawGraphics = new DrawLine();
  drawGraphics.draw(canvas);
  drawGraphics = new DrawRect();
  drawGraphics.draw(canvas);
 }

 @Override
 public void run() {
  while (!Thread.currentThread().isInterrupted()) {

   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
   }

   // 使用postInvalidate 可以直接在线程中更新界面
   postInvalidate();
  }
 }
 
 public void setCircleColor(int circleColor) {
  mCircleColor = circleColor;
 }
 
 public int getCircleColor() {
  return mCircleColor;
 }
 
 public void setLineColor(int lineColor) {
  mLineColor = lineColor;
 }
 
 public int getLineColor() {
  return mLineColor;
 }
 
 public void setRectColor (int rectColor) {
  mRectColor = rectColor;
 }
 
 public int getRectColor() {
  return mRectColor;
 }
 
 static class DrawCircle implements DrawGraphics {

  private Paint paint = null;
  private Paint paint_eye = null;

  public DrawCircle() {
   paint = new Paint();
   paint_eye = new Paint();
  }

  @Override
  public void draw(Canvas canvas) {

   // 绘制圆形(圆心x,圆心y,半径r,画笔p)
   paint_eye.setAntiAlias(true);
   paint.setAntiAlias(true);

   RectF rectF = new RectF(120/2, 60/2, 370/2, 240/2);

   paint_eye.setColor(Color.BLACK);
   paint.setColor(mCircleColor);

   canvas.drawCircle(190/2, 110/2, 18/2, paint_eye);
   canvas.drawCircle(300/2, 110/2, 18/2, paint_eye);
   canvas.drawArc(rectF, 180, 180, true, paint);
  }
 }
 
 static class DrawLine implements DrawGraphics {

  private Paint paint = null;
  public DrawLine() {
   paint = new Paint();
  }

  @Override
  public void draw(Canvas canvas) {
   paint.setAntiAlias(true);

   // 绘制直线
   paint.setColor(mLineColor);

   // 设置线条粗细
   paint.setStrokeWidth(12);

   canvas.drawLine(120/2, 40/2, 170/2, 90/2, paint);
   canvas.drawLine(320/2, 90/2, 370/2, 40/2, paint);
  }
 }

 static class DrawRect implements DrawGraphics {

  private Paint paint = null;

  public DrawRect() {
   paint = new Paint();
  }

  @Override
  public void draw(Canvas canvas) {
   RectF rectF1 = new RectF(120/2, 170/2, 370/2, 500/2);
   RectF rectF2 = new RectF(40/2, 150/2, 90/2, 400/2);
   RectF rectF3 = new RectF(390/2, 150/2, 440/2, 400/2);
   RectF rectF4 = new RectF(140/2, 520/2, 200/2, 650/2);
   RectF rectF5 = new RectF(290/2, 520/2, 350/2, 650/2);
   
   paint.setAntiAlias(true);

   // 设置画笔颜色为BLUE
   paint.setColor(mRectColor);

   // 在画布上绘制圆角矩形/圆弧/直线
   canvas.drawRoundRect(rectF1, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF2, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF3, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF4, 20/2, 20/2, paint);
   canvas.drawRoundRect(rectF5, 20/2, 20/2, paint);
  }
 }
}
 

2 提取TestCustom中自定义控件CustomTextView、GameView文件,并以CustomView.jar形式导出。

右击TestCustom——选中Export,将弹出下图:

 

如图选中JAR file,单击Next,如下图所示:

 

注意:右框中的最好都处于不选状态,左框中的视实际情况而定,有时自定义控件中需要用到一些资源文件,则res目录中相应的资源文件必须选中。JAR file框中应填写该jar文件的保存完整路径及jar文件名,这里为G:\huangqin\CustomView.jar 。

一直单击Next,知道Finish。

 

3 将CustomView.jar拷贝到Athrun framework中的libs目录下。

 

4 根据TestCustom中自定义控件CustomTextView、GameView的定义,在framework添加相应的映射文件CustomViewElement、GameViewElement。

 

 其中

CustomViewElement.java

package org.athrun.android.framework.viewelement;

import android.app.Instrumentation;
import android.view.View;
import com.example.testcustom.*;

public class CustomViewElement extends ViewElement{ 
 
 private CustomTextView mCustomView;
 protected CustomViewElement(Instrumentation inst, CustomTextView view) {
  super(inst, view);
  mCustomView = view;
 }
 
 public void clearText() {
  setText("");
 }

 public void setText(final String newText) {
  inst.waitForIdleSync();
  ViewUtils.scrollInToScreenIfNeeded(inst, mCustomView);
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {

   @Override
   public void run() {
    mCustomView.setText(newText);
   }
  });
  inst.waitForIdleSync();
 }
 
 public String getText() {
  inst.waitForIdleSync();
  ViewUtils.scrollInToScreenIfNeeded(inst, mCustomView);
  inst.waitForIdleSync();
  return mCustomView.getText().toString();
 }
}

GameViewElement.java

package org.athrun.android.framework.viewelement;

import android.app.Instrumentation;

import com.example.testcustom.*;

public class GameViewElement extends ViewElement{

 private GameView mGameView = null;
 protected GameViewElement(Instrumentation inst, GameView view) {
  super(inst, view);
  mGameView = view;
 }
 
 public void setCircleColor(final int circleColor) {
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {

   @Override
   public void run() {
    mGameView.setCircleColor(circleColor);
   }   
  });  
 }
 
 public void setLineColor (final int lineColor) {
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {

   @Override
   public void run() {
    mGameView.setLineColor(lineColor);
   }   
  });  
 }
 
 public void setRectColor (final int rectColor){
  inst.waitForIdleSync();
  inst.runOnMainSync(new Runnable() {

   @Override
   public void run() {
    mGameView.setRectColor(rectColor);
   }   
  });  
 }
}

5 通过ant jar重新编译framework.jar 。

 

6 将编译好的framework.jar文件拷贝到测试程序TestAthrunCustomView的libs目录下。然后引用自定义控件的映射文件CustomViewElement、GameViewElement进行功能自动化测试。

 

 

其中

MainActivityTest.java

package com.dragon.android.pandaspacetest;

import org.athrun.android.framework.AthrunTestCase;
import org.athrun.android.framework.Test;
import org.athrun.android.framework.ViewOperation.Direction;
import org.athrun.android.framework.viewelement.AbsListViewElement;
import org.athrun.android.framework.viewelement.CustomViewElement;
import org.athrun.android.framework.viewelement.GameViewElement;
import org.athrun.android.framework.viewelement.ViewGroupElement;

import android.graphics.Color;

public class MainActivityTest extends AthrunTestCase{

 public MainActivityTest() throws Exception {
  super("com.example.testcustom", "com.example.testcustom.MainActivity");
  AthrunTestCase.setMaxTimeToFindView(10000);
 }
 
 @Test
 public void testWaitForActivity() throws Exception{
  assertEquals(true, this.getDevice().waitForActivity("MainActivity", 3000));   
 }
 
 @Test
 public void testSetText() throws Exception {
  
  
  CustomViewElement customView = this.findElementById("custome_view", CustomViewElement.class);
  customView.setText("Custom Successfully!");  
  String result = customView.getText();
  assertEquals(result, "Custom Successfully!");
  assertNotSame(result, "Custom Failed!");
 }
 
 @Test
 public void testSetGameViewLineColor() throws Exception {
  GameViewElement gameView = this.findElementById("game_view", GameViewElement.class);
  gameView.setCircleColor(Color.YELLOW);
  gameView.setRectColor(Color.RED);
    
 }

}


    
[2] 对View DrawingCache的了解
    来源: 互联网  发布时间: 2014-02-18
对View DrawingCache的理解

View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有

 

void setDrawingCacheEnabled(boolean flag),
Bitmap getDrawingCache(boolean autoScale),
void buildDrawingCache(boolean autoScale),
void destroyDrawingCache()

 

 

 

我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,然后再调用getDrawingCache方法就可 以获得view的cache图片了。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果 cache没有建立,系统会自动调用buildDrawingCache方法生成cache。若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。 当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。

ViewGroup在绘制子view时,而外提供了两个方法

 

void setChildrenDrawingCacheEnabled(boolean enabled)
setChildrenDrawnWithCacheEnabled(boolean enabled)

 

 

setChildrenDrawingCacheEnabled方法可以使viewgroup里所有的子view开启cache,setChildrenDrawnWithCacheEnabled使在绘制子view时,若该子view开启了cache, 则使用它的cache进行绘制,从而节省绘制时间。 获取cache通常会占用一定的内存,所以通常不需要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。


    
[3] NSFileManager 资料应用
    来源: 互联网  发布时间: 2014-02-18
NSFileManager 文件应用
- (void)clearDisk
{
    dispatch_async(self.ioQueue, ^
    {
        [[NSFileManager defaultManager] removeItemAtPath:self.diskCachePath error:nil];
        [[NSFileManager defaultManager] createDirectoryAtPath:self.diskCachePath
                                  withIntermediateDirectories:YES
                                                   attributes:nil
                                                        error:NULL];
    });
}

- (void)cleanDisk
{
    dispatch_async(self.ioQueue, ^
    {
        NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge];
        NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];
        for (NSString *fileName in fileEnumerator)
        {
            NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
            NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
            if ([[[attrs fileModificationDate] laterDate:expirationDate] isEqualToDate:expirationDate])
            {
                [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
            }
        }
    });
}

-(int)getSize
{
    int size = 0;
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];
    for (NSString *fileName in fileEnumerator)
    {
        NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        size += [attrs fileSize];
    }
    return size;
}

- (int)getDiskCount
{
    int count = 0;
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];
    for (NSString *fileName in fileEnumerator)
    {
        count += 1;
    }
    
    return count;
}

 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android录音应用实例教程 iis7站长之家
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3