ImageView EditText 使用详解

ImageView 640?wx_fmt=gif

640?wx_fmt=other

极力推荐文章:欢迎收藏Android 干货分享 

640?wx_fmt=other

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

一、ImageView 的继承关系

一、ImageView 的继承关系

ImageView的继承关系 如下:

java.lang.Object   
       ↳ android.view.View      
                 ↳ android.widget.ImageView

二、ImageView 常用方法

ImageView主要用于显示图像资源,BitmapDrawable资源,同时也常用于图片渲染调色,图片缩放剪裁等。

以下XML代码段是使用ImageView显示图像资源的常见示例:

  1. xml使用ImageView 控件

 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
     <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@mipmap/ic_launcher"
         />
 </LinearLayout>
 

三、 ImageView 背景 间距属性设置

  1. xml使用ImageView控件

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/grey"
        android:padding="5dp" 
        android:src="@drawable/ic_launcher" />
  1. 实现效果如下:

640?wx_fmt=other

padding background 属性设置

四、 使用Bitmap 类型动态设置ImageView 资源

  1. xml 使用ImageView控件

<ImageView
        android:id="@+id/img_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />
  • 2.java 类实现

        // 1.从资源中获取Bitmap
        ImageView mImageView1 = (ImageView) findViewById(R.id.img_1);
        DrawableUtils.UseBitmap(this, mImageView1, R.drawable.gril);
  • 3.DrawableUtils类方法实现


    // 1.从资源中获取Bitmap
    public static void UseBitmap(Context context, ImageView imageView, int drawableId) {

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                drawableId);
        imageView.setImageBitmap(bitmap);

    }
  1. 实现效果如下:

640?wx_fmt=other

bitmap 类型的图片

五、ImageView 图片倒影实现

  1. xml使用ImageView 控件

    <ImageView
        android:id="@+id/img_4"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/grey"
        android:padding="5dp" />
  1. java代码 实现效果

        // 4.倒影图片
        ImageView mImageView4 = (ImageView) findViewById(R.id.img_4);
        mImageView4.setImageBitmap(DrawableUtils.CreateReflectionImageWithOrigin(
                DrawableUtils.DrawableToBitmap(getResources().getDrawable(
                        R.drawable.img1))));
  1. DrawableUtils 工具类的方法实现

    // 5. Drawable----> Bitmap
    public static Bitmap DrawableToBitmap(Drawable drawable) {

        // 获取 drawable 长宽
        int width = drawable.getIntrinsicWidth();
        int heigh = drawable.getIntrinsicHeight();

        drawable.setBounds(0, 0, width, heigh);

        // 获取drawable的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 创建bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
        // 创建bitmap画布
        Canvas canvas = new Canvas(bitmap);
        // 将drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }
  1. 实现效果如下:

640?wx_fmt=other

ImageView 倒影功能实现

六、ImageView 图片缩放实现

  1. xml使用ImageView 控件

    <ImageView
        android:id="@+id/img_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" />
  1. java代码 实现效果

        // 2. 图片缩放
        ImageView mImageView2 = (ImageView) findViewById(R.id.img_2);
        mImageView2.setImageDrawable(DrawableUtils.ZoomDrawable(getResources().getDrawable(R.drawable.img1),
                240, 200));
  1. DrawableUtils 工具类方法实现

    // 9. drawable进行缩放 ---> bitmap 然后比对bitmap进行缩放
    public static Drawable ZoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        // 调用5 中 drawable转换成bitmap
        Bitmap oldbmp = DrawableToBitmap(drawable);

        // 创建操作图片用的Matrix对象
        Matrix matrix = new Matrix();
        // 计算缩放比例
        float sx = ((float) w / width);
        float sy = ((float) h / height);
        // 设置缩放比例
        matrix.postScale(sx, sy);
        // 建立新的bitmap,其内容是对原bitmap的缩放后的图
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
                matrix, true);
        return new BitmapDrawable(newbmp);
    }

  1. 实现效果如下:

640?wx_fmt=other

Imageview 缩放

七、ImageView 圆角图片 实现

  1. xml使用ImageView 控件

    <ImageView
        android:id="@+id/img_3"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@color/grey"
        android:padding="5dp" />
  • 2.java代码 实现效果

        // 3. 圆角图片
        ImageView mImageView3 = (ImageView) findViewById(R.id.img_3);
        mImageView3.setImageBitmap(DrawableUtils.SetRoundCornerBitmap(
                DrawableUtils.DrawableToBitmap(getResources().getDrawable(
                        R.drawable.img1)), 60));
  1. DrawableUtils工具类方法实现

    // 6.圆角图片
    public static Bitmap SetRoundCornerBitmap(Bitmap bitmap, float roundPx) {
        int width = bitmap.getWidth();
        int heigh = bitmap.getHeight();
        // 创建输出bitmap对象
        Bitmap outmap = Bitmap.createBitmap(width, heigh,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outmap);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, width, heigh);
        final RectF rectf = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return outmap;
    }
  1. 实现效果如下:

640?wx_fmt=other

圆角图片实现

八、BitmapDrawable 转换工具类

BitmapDrawable 转换常用工具类源代码如下:

package com.programandroid.Utils;

import java.io.ByteArrayOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

/*
 * DrawableUtils.java
 *
 *  Created on: 2017-10-24
 *      Author: wangjie
 * 
 *  Welcome attention to weixin public number get more info
 *
 *  WeiXin Public Number : ProgramAndroid
 *  微信公众号 :程序员Android
 *
 */
public class DrawableUtils {

    // 1.从资源中获取Bitmap
    public static void UseBitmap(Context context, ImageView imageView, int drawableId) {

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
                drawableId);
        imageView.setImageBitmap(bitmap);

    }

    // 2.Bitmap ---> byte[]
    public byte[] BitmapToBytes(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    // 3.byte[] ---->bitmap
    public Bitmap BytesToBitmap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        } else {
            return null;
        }
    }

    // 4.Bitmap 缩放方法
    public static Bitmap ZoomBitmap(Bitmap bitmap, int width, int heigh) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scalewidth = (float) width / w;
        float scaleheigh = (float) heigh / h;
        matrix.postScale(scalewidth, scaleheigh);
        Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newBmp;

    }

    // 5. Drawable----> Bitmap
    public static Bitmap DrawableToBitmap(Drawable drawable) {

        // 获取 drawable 长宽
        int width = drawable.getIntrinsicWidth();
        int heigh = drawable.getIntrinsicHeight();

        drawable.setBounds(0, 0, width, heigh);

        // 获取drawable的颜色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 创建bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
        // 创建bitmap画布
        Canvas canvas = new Canvas(bitmap);
        // 将drawable 内容画到画布中
        drawable.draw(canvas);
        return bitmap;
    }

    // 6.圆角图片
    public static Bitmap SetRoundCornerBitmap(Bitmap bitmap, float roundPx) {
        int width = bitmap.getWidth();
        int heigh = bitmap.getHeight();
        // 创建输出bitmap对象
        Bitmap outmap = Bitmap.createBitmap(width, heigh,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outmap);
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, width, heigh);
        final RectF rectf = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return outmap;
    }

    // 7.获取带倒影的图片
    public static Bitmap CreateReflectionImageWithOrigin(Bitmap bitmap) {

        final int reflectionGapLine = 4;
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
                h / 2, matrix, false);

        Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bitmap, 0, 0, null);
        Paint deafalutPaint = new Paint();
        canvas.drawRect(0, h, w, h + reflectionGapLine, deafalutPaint);

        canvas.drawBitmap(reflectionImage, 0, h + reflectionGapLine, null);

        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGapLine, 0x70ffffff,
                0x00ffffff, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
                + reflectionGapLine, paint);
        return bitmapWithReflection;
    }

    // 8. bitmap ---Drawable
    public static Drawable BitmapToDrawable(Bitmap bitmap, Context context) {
        BitmapDrawable drawbale = new BitmapDrawable(context.getResources(),
                bitmap);
        return drawbale;
    }

    // 9. drawable进行缩放 ---> bitmap 然后比对bitmap进行缩放
    public static Drawable ZoomDrawable(Drawable drawable, int w, int h) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        // 调用5 中 drawable转换成bitmap
        Bitmap oldbmp = DrawableToBitmap(drawable);

        // 创建操作图片用的Matrix对象
        Matrix matrix = new Matrix();
        // 计算缩放比例
        float sx = ((float) w / width);
        float sy = ((float) h / height);
        // 设置缩放比例
        matrix.postScale(sx, sy);
        // 建立新的bitmap,其内容是对原bitmap的缩放后的图
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
                matrix, true);
        return new BitmapDrawable(newbmp);
    }

}

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

一、EditText 继承关系

一、EditText 继承关系

EditText继承关系 如下:

java.lang.Object    
       ↳ android.view.View     
              ↳ android.widget.TextView      
                      ↳ android.widget.EditText

二、EditText 常用举例

EditText主要用于输入和修改文本内容。

<EditText
      android:id="@+id/plain_text_input"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:inputType="text"/>

三、EditText 自定义背景框

  1. xml 中使用EditText 控件

    <!-- 自定义EditText 背景 -->

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/custom_edittext_background"
        android:gravity="center"
        android:hint="一、自定义EditText背景框"
        android:padding="8dp"
        android:textSize="16sp" />

  1. 自定义 EditText 背景框

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 圆角-->
    <corners android:radius="5dp" />
    <!--描边-->
    <stroke
        android:width="1dp"
        android:color="@android:color/holo_blue_light" />

</shape>
  1. 实现效果

640?wx_fmt=other

自定义背景框实现

四、EditText自动检测输入内容

  1. xml 中使用EditText 控件

    <!-- 自动检测输入更正 -->

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoText="true"
        android:hint="二、自动检测输入更正属性 autoText"
        android:textColor="#FF6100" />
  1. 实现效果

640?wx_fmt=other

自动检测输入正确性

五、Edittext 密文显示

  1. xml 中使用EditText 控件

    <!-- 以密文的形式显示 -->

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="三、以密文的形式显示密码"
        android:password="true" />

  1. 实现效果

640?wx_fmt=other

密文显示属性

六、EditText 限制只能输入特定字符

限定只能输入阿拉伯数字实现如下:

  1. xml 中使用EditText 控件

   <!-- 设置允许输入的字符 -->

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:digits="123456789.+-*/\n()"
        android:hint="四、设置限制允许输入阿拉伯数字" />

  1. 实现效果

640?wx_fmt=other

限定只能输入阿拉伯数字

640?wx_fmt=jpeg

640?wx_fmt=jpeg

长按识别二维码,领福利

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵

为您每个 “在看”  而怦然心动640?wx_fmt=gif

posted @ 2019-06-13 10:02  程序员Android的博客  阅读(63)  评论(0编辑  收藏  举报