Image reflection in android

Source: Internet
Author: User

1 ,:



2. Core code:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> package com. example. pic_reflection; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. linearGradient; import android. graphics. matrix; import android. graphics. paint; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. graph Ics. shader. tileMode; import android. graphics. drawable. bitmapDrawable; import android. OS. bundle; import android. widget. imageView; public class MainActivity extends Activity {private ImageView img = null; private ImageView orginalImg; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); this. orginalImg = (ImageView) this. findV IewById (R. id. img); this. orginalImg. setBackgroundResource (R. drawable. b); this. img = (ImageView) this. findViewById (R. id. reflection1); this. img. setImageBitmap (createReflectedImage (BitmapDrawable) this. getResources (). getDrawable (R. drawable. B )). getBitmap (), 110);} public static Bitmap createReflectedImage (Bitmap originalImage, int reflectionHeight) {int width = originalImage. getWidth (); int height = OriginalImage. getHeight (); Matrix matrix = new Matrix (); // implements the 90-degree matrix for image flip. preScale (1,-1); if (reflectionHeight> height) reflectionHeight = height; // create a reflection image (half the size of the original image) Bitmap reflectionImage = Bitmap. createBitmap (originalImage, 0, height-reflectionHeight, width, reflectionHeight, matrix, false); // create a reflection image Bitmap finalReflection = Bitmap. createBitmap (width, reflectionHeight, Config. ARGB _ 8888); // create a Canvas canvas Canvas = new canvas (finalReflection); // Canvas. drawBitmap (originalImage, 0, 0, null); // draws the reflected image to the canvas. drawBitmap (reflectionImage, 0, 0, null); Paint shaderPaint = new Paint (); // create a linear gradient LinearGradient object LinearGradient shader = new LinearGradient (0, 0, 0, finalReflection. getHeight () + 1, 0x70ffffff, 0x00ffffff, TileMode. MIRROR); shaderPaint. setShader (shader); shaderPaint. SetXfermode (new porterduxfermode (Mode. DST_IN); // draw the area of the reversed image size on the canvas, and add the gradient effect to it. The image reflection effect is displayed. Canvas. drawRect (0, 0, width, finalReflection. getHeight (), shaderPaint); return finalReflection ;}}3. main. xml

 
     
      
  
 

4. activity_main.xml

 <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "wrap_content" android: layout_height = "wrap_content">
     
 </FrameLayout>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.