格式化字串長度,超出部分顯示省略符號,區分漢字跟字母

來源:互聯網
上載者:User

     由於android軟體的顯示控制項上要求長度超過一定的數值就截取並加省略符號,所以寫了這個方法:

    package com.eoeandroid.demo.testcode;    import android.app.Activity;    import android.graphics.Bitmap;    import android.graphics.BitmapFactory;    import android.graphics.Matrix;    import android.graphics.drawable.BitmapDrawable;    import android.os.Bundle;    import android.view.ViewGroup.LayoutParams;    import android.widget.ImageView;    import android.widget.LinearLayout;    import android.widget.ImageView.ScaleType;    public class bitmaptest extends Activity {    public void onCreate(Bundle icicle) {            super.onCreate(icicle);            setTitle("eoeAndroid教程: 縮放和旋轉圖片 -by:IceskYsl");            LinearLayout linLayout = new LinearLayout(this);            // 載入需要操作的圖片,這裡是eoeAndroid的logo圖片            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),                   R.drawable.eoe_android);            //擷取這個圖片的寬和高            int width = bitmapOrg.getWidth();            int height = bitmapOrg.getHeight();            //定義預轉換成的圖片的寬度和高度            int newWidth = 200;            int newHeight = 200;            //計算縮放率,新尺寸除原始大小            float scaleWidth = ((float) newWidth) / width;            float scaleHeight = ((float) newHeight) / height;            // 建立操作圖片用的matrix對象            Matrix matrix = new Matrix();            // 縮放圖片動作            matrix.postScale(scaleWidth, scaleHeight);            //旋轉圖片 動作            matrix.postRotate(45);            // 建立新的圖片            Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,                              width, height, matrix, true);            //將上面建立的Bitmap轉換成Drawable對象,使得其可以使用在ImageView, ImageButton中            BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);            //建立一個ImageView            ImageView imageView = new ImageView(this);            // 設定ImageView的圖片為上面轉換的圖片            imageView.setImageDrawable(bmd);            //將圖片置中顯示            imageView.setScaleType(ScaleType.CENTER);            //將ImageView添加到布局模板中            linLayout.addView(imageView,              new LinearLayout.LayoutParams(                          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT                    )            );            // 設定為本activity的模板            setContentView(linLayout);        }    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.