跳到主要內容

[Android]在圖上畫圖與dp<=>px轉換

有兩種做法:
1.以Canvas畫在指定位址與縮放,再將Canvas所畫的Bitmap拿來用
2.以Layout檔Inflate,再將View轉為Bitmap拿來用
結論:
第二種方法較快且容易擴充與調整

方法一:
Bitmap org = BitmapFactory.decodeResource(getResources(), R.drawable.empty_pinpoint);
Bitmap pinPoint = Bitmap.createScaledBitmap(org, dp2px(52), dp2px(68), true);
Canvas pinPointCanvas = new Canvas(pinPoint);

Bitmap head = BitmapFactory.decodeResource(getResources(), R.drawable.test_head);
pinPointCanvas.drawBitmap(head, null, new Rect(dp2px(6), dp2px(3), dp2px(46), dp2px(43)), null);

ImageView view = (ImageView) findViewById(R.id.imgResult);
view.setImageBitmap(pinPoint);


方法二:

        //1. Make a bitmap to draw to.
        Bitmap bmp = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);

        //2. We need a Canvas to handle drawing to the bitmap. Bitmap has no draw methods of its own.
        Canvas canvas = new Canvas(bmp);

        //3. Get a reference to the LayoutInflater.
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        //4. Inflate the layout we want to draw.
        View layout = inflater.inflate(R.layout.layout_to_draw, null);

        //5. Call setDrawingCacheEnabled to true, since the default is false. 
        layout.setDrawingCacheEnabled(true);

        //6. Tell the layout how big it should be. In this case we are using the full size of the canvas.
        layout.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));

        //7. Now we apply the measures so that it embiggens to the correct size before drawing.
        layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());

        //8. Finally, we draw the bitmap. Mission accomplished. But of course we still can't see the bitmap. So...
        canvas.drawBitmap(layout.getDrawingCache(), 0, 0, null);

        //9. Get reference to an ImageView that you want to hold your bitmap. 
        ImageView view = (ImageView) findViewById(R.id.image_to_show);

        //10. Set the bitmap as the ImageView's Image Bitmap.
        view.setImageBitmap(bmp);



dp<=>px轉換

    private int dp2px(int dp) {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        return (int)((dp * displayMetrics.density) + 0.5);
    }

留言

這個網誌中的熱門文章

Run Android VTS with Android R emulator through WSL

Background Android VTS The Android Vendor Test Suite (VTS) provides extensive new functionality for Android testing and promotes a test-driven development process. Android R emulator Run the latest Android R image from Google CI build artifacts WSL WSL is a collection of components that enables native Linux ELF64 binaries to run on Windows . It contains both user mode and kernel mode components. It is primarily comprised of: User mode session manager service that handles the Linux instance life cycle Pico provider drivers (lxss.sys, lxcore.sys) that emulate a Linux kernel by translating Linux syscalls Pico processes that host the unmodified user mode Linux (e.g. /bin/bash) It is the space between the user mode Linux binaries and the Windows kernel components where the magic happens. By placing unmodified Linux binaries in Pico processes we enable Linux system calls to be directed into the Windows kernel. The lxss.sys and lxcore.sys drivers translate the Linux s...

[Qt][Embedded][PXA270]
Porting Qt 4.8.6 for Creator XScale PXA270

這篇文章介紹這學期我在嵌入式作業系統課程,使用Creator XScale PXA270板子的一些學習筆記與製作final project的一些過程,歡迎各位多多指教~ 背景介紹 Creator XScale PXA270開發版是一塊由 新華電腦(microtime) 公司出品的 模組化XScale/ARM/SoC/FPGA/DSP嵌入式行動通訊發展平台 ,在此課程與final project主要使用的是"Create XScale‐PXA270 子板"與"MTLCD‐0708048 LCD Module",在Create XScale‐PXA270子板上的為Intel(現在已出售給Marvell)的 Xscale系列 PXA270 處理器,基於ARMv5TE架構指令集,在旁附有SDRAM Memory 64MB、Flash Memory 32MB、SD Card Connectors、RJ45 10/100 Base‐T Ethernet Interface、USB 1.1 Device/Host Port各一、AC97 Audio Codec(Line_in、Mic_in、Headphone)、ADC Interface*4、PWM Interface、CMOS Camera Interface、TFT-LCD Interface、GPIO Interface...等;在軟體功能部分,由新華科技提供了GNU GCC cross compiler toolchain(arm-unknown-linux-gnu-gcc 4.0.2)、Uboot 1.1.5、Linux Kernel 2.6.15.3與patch(提供支援子板上的Ethernet、USB Host、TFT‐LCD(Frame buffer、Touch Screen)、AC97‐Codec...等與母板LED、7‐Seg LED、Key Pad...等的device driver)、rootfs,相關的實驗環境設定可以參考 User Guide 。 看到以上這麼多的軟硬體介面,別以為這板子有多強,其實這是 2006 年 ASUS P535 手機使用的規格了,不禁讓人感嘆科技進步之快阿~ 在此附上開發版本尊 Qt 是自由且開放原始碼的跨平台C...

[Android][AOSP][ddmlib][Intelij IDEA][Gradle]搭建Android ddmlib編譯環境

Android ddmlib在AOSP的platform/tools/base的repo內 可以使用以下指令將整個repo的master branch抓回: git clone https://android.googlesource.com/platform/tools/base 取其中的annotations、common、ddmlib 建立Intelij IDEA專案 使用Intelij IDEA開一新的Java Project,GroupId、ArtifictedId、Version隨便,並使用graddle wrapper方式建立專案: GroupId 'com.android.tools.ddms' ArtifictedId = 'ddmlib' Version '1.0-SNAPSHOT' 刪除IDE建立的src資料夾,建立base資料夾,將annotations、common、ddmlib複製進來 參考Android Studio建立的專案,修改build.gradle為: // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { mavenCentral() } } allprojects { repositories { mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir } 修改settings.gradle,加入annotations、common、ddmlib為include rootProject.name = 'ddmlib' include ':base:annotations' include ':base:common' include ':base:ddmlib' 修改annotations、common、ddmlib內的b...