Android学习笔记1

1.View添加了shape,动态设置shape的颜色

GradientDrawable myGrad = (GradientDrawable) view.getBackground();
			myGrad.setColor(Color.RED);
			

2.通过路径计算区域

/** 
	 * 通过路径计算区域 
	 *  
	 * @param path 
	 *            路径对象 
	 * @return 路径的Region 
	 */
	private Region computeRegion(Path path) {
		Region region = new Region();
		RectF f = new RectF();
		path.computeBounds(f, true);
		region.setPath(path, new Region((int) f.left, (int) f.top, (int) f.right, (int) f.bottom));
		return region;
	}

3.android的adjustViewBounds:
android的adjustViewBounds属于View的属性层,在onMeasure时通过控制View的宽高比来影响View的大小。而宽度比是根据加载的图片的宽高算的。也就是说可以通过设置adjustViewBounds为true,来调整View的宽度比等于加载图片的宽度比。这往往会结合scaleType=centerCrop用于等比例显示图片

4.如何去除安卓EditText中的底部横线
Android:background=“@null”

5.软键盘判断与控制:

public void ShowKeyBord() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                ShowSoftInput();
            }
        }, 100);
    }

    private void ShowSoftInput() {
        mNoticeInput.setFocusable(true);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 显示软键盘
        imm.showSoftInput(mNoticeInput, 0);
    }

    private void HideSoftInput() {
        if (Build.VERSION.SDK_INT >= 17) {
            if (!isSoftShowing()) {
                return;
            }
        }
        // 隐藏软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);

    }


    private int getSoftButtonsBarHeight() {
        DisplayMetrics metrics = new DisplayMetrics();
        //这个方法获取可能不是真实屏幕的高度
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        //获取当前屏幕的真实高度
        getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight) {
            return realHeight - usableHeight;
        } else {
            return 0;
        }
    }

    private boolean isSoftShowing() {
        //获取当前屏幕内容的高度
        int screenHeight = getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        return screenHeight - rect.bottom - getSoftButtonsBarHeight() != 0;
    }

6.Popupwindow

Popupwindow如果需要点击空白处自动消失,需要设置两个函数:setFocusable(true); setBackgroundDrawable(new BitmapDrawable());(感觉有点奇葩,BackgroundDrawable不设就不会起效)

7.获取文件大小

getcontentlength返回值小文件是正常的,但是大文件就返回-1:2.2版本以上HttpURLConnection跟服务交互采用了"gzip"压缩:参考api:By default, this implementation of HttpURLConnection requests that servers use gzip compression. Since getContentLength() returns the number of bytes transmitted, you cannot use that method to predict how many bytes can be read from getInputStream(). Instead, read that stream until it is exhausted: whenread() returns -1.
取消办法这http request的head中设置如下参数即可:connection.setRequestProperty(“Accept-Encoding”, “identity”);

8.seakbar滑动条大小
自定义seakbar的时候,应该设置seakbar的最小与最大宽度与滑动块的高度相关比例的高度,不然显示时出现显示不全的情况

9.ldpi、mdpi、hdpi、xhdpi、xxhdpi比例关系

low:medium:high:extra-high:extra-extra-high=3:4:6:8:12

就是说五个文件夹的比例为3:4:6:8:12。具体是怎么回事呢?下面分析一下:
比如我用一个480800的4寸手机,这个手机的屏幕密度按照Google的说法,就属于密度为high level的水平(通过分辨率和屏幕尺寸计算密度,然后google自己有一套标准说你位于哪个范围属于哪个level的密度水平),然后这个手机的应用在用图片的时候,就会去hdpi下去找,并且以这个文件夹的图片为标准,也就是说比如我的应用去取一张aa.png的图片,这个图片的原图尺寸为3030,恰好hdpi下有一张,那这张图片显示到屏幕上以后,它的显示尺寸长宽都为30px。那问题来了,但如果我的hdpi下没有这张图片,而只在xhdpi下有这张图片,图片的原图尺寸是3030,那请问显示到屏幕上的图片的尺寸会是多大呢,还是长宽都为30px吗?
答案是否定的,而且现在就用到了上面那个比例,high:extra-high=6:8。先明确这样一个问题,如果我的屏幕是hdpi的,结果我的图片是放到了xhdpi下,那系统会把这张图片进行缩小显示,也就是说我的xhdpi下放了一张30
30的图片,那显示当hdpi屏幕上肯定要比30*30小,这样才能保证说大小屏幕界面显示效果是一致的,因为密度小的手机显示一张图片要比密度大的手机显示同一张图片的面积要大,要想显示面积一样就必须要把图片搞小点。接上面的问题,
假设显示在我的hdpi屏幕上的图片的宽度为x,那满足以下等式:
6:8=x:30 。
可得到x=22.3,向后取整数得23 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值