/** 初始化Bitmap */ private void createBitmaps() { mSky = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.sky); mSky = Bitmap.createScaledBitmap(mSky, mScreenWidth, mSkyHeight, true); mTown = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.buildings); mTown = Bitmap.createScaledBitmap(mTown, mScreenWidth, mTownHeight, true); mSun = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.sun); mSun = Bitmap.createScaledBitmap(mSun, mSunSize, mSunSize, true); }
@Override public void onDraw(Canvas canvas) { loadBitmap(); if (image != null) { shader = new BitmapShader( Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); paint.setShader(shader); int circleCenter = viewWidth / 2; canvas.drawCircle( circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder); if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { canvas.drawCircle( circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 2.0f, paint); } else { canvas.drawCircle( circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint); } // canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - // 4.0f, paint); } }
public static Bitmap[] load(String src, int width, int height) { Bitmap[] texture = null; InputStream is = null; AssetManager am = resources.getAssets(); String[] nameOfBoxs = null; try { nameOfBoxs = am.list(src); } catch (IOException e) { Log.d("GraphicMaster", "Culd not find folder " + src); } Log.d("GraphicMaster", "NameOfBoxs.length = " + nameOfBoxs.length); texture = new Bitmap[nameOfBoxs.length]; for (int i = 0; i < nameOfBoxs.length; i++) { try { is = resources.getAssets().open(src + "/" + nameOfBoxs[i]); texture[i] = BitmapFactory.decodeStream(is); /* if(width > 0 && height > 0) texture[i] = Bitmap.createScaledBitmap(texture[i], width, height, false);*/ Log.d( "GraphicMaster", "Scaled from " + new Integer((int) (texture[i].getWidth())).toString() + " " + new Integer((int) (texture[i].getHeight())).toString() + " to " + new Integer((int) (texture[i].getWidth() * GameSettings.widthM)).toString() + " " + new Integer((int) (GameSettings.heightM * texture[i].getWidth())).toString()); Log.d( "GraphicMaster", "Scale" + new Double(GameSettings.widthM).toString() + " " + new Double(GameSettings.heightM).toString()); if (GameSettings.widthM != 1 && GameSettings.heightM != 1) texture[i] = Bitmap.createScaledBitmap( texture[i], (int) (texture[i].getWidth() * GameSettings.widthM), (int) (texture[i].getHeight() * GameSettings.heightM), false); } catch (IOException e) { Log.d("GraphicMaster", "Culd not read: " + src + nameOfBoxs[i]); } } return texture; }
/** @Method: packData @Description: ���Ҫ���͵���� */ private void packData() { currentBitmap = BitmapList[firstView]; ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); // stream1 currentBitmap.compress( Bitmap.CompressFormat.PNG, 1, stream1); // compress to which format you want byte[] byte_arr = stream1.toByteArray(); // stream1 to byte try { stream1.close(); stream1 = null; } catch (IOException e1) { e1.printStackTrace(); } String image_str = Base64.encodeBytes(byte_arr); // byte to string Bitmap smallcurrentBitmap; float wRatio = (float) (currentBitmap.getWidth() / 100.0); float hRatio = (float) (currentBitmap.getHeight() / 100.0); if (wRatio > 1 && hRatio > 1) // ����ָ����С������С��Ӧ�ı��� { float scaleTemp; if (wRatio > hRatio) scaleTemp = hRatio; else scaleTemp = wRatio; wRatio = currentBitmap.getWidth() / scaleTemp; hRatio = currentBitmap.getHeight() / scaleTemp; int h = (int) wRatio; int w = (int) hRatio; smallcurrentBitmap = Bitmap.createScaledBitmap(currentBitmap, h, w, false); } else smallcurrentBitmap = currentBitmap; ByteArrayOutputStream stream2 = new ByteArrayOutputStream(); // stream2 smallcurrentBitmap.compress( Bitmap.CompressFormat.PNG, 50, stream2); // compress to which format you want smallcurrentBitmap.recycle(); byte_arr = stream2.toByteArray(); // stream2 to byte try { stream2.close(); stream2 = null; } catch (IOException e) { e.printStackTrace(); } String smallimage_str = Base64.encodeBytes(byte_arr); // byte to string nameValuePairs.add(new BasicNameValuePair("protocol", "upload")); // ��װ��ֵ�� nameValuePairs.add(new BasicNameValuePair("id", LoginActivity.mineID)); nameValuePairs.add(new BasicNameValuePair("albumName", choosedAlbum)); nameValuePairs.add( new BasicNameValuePair( "imageName", PhotoAlbumActivity.AlbumsFloderName.get(AlbumName).get(currentNum))); Log.d("debug", PhotoAlbumActivity.AlbumsFloderName.get(AlbumName).get(currentNum)); nameValuePairs.add(new BasicNameValuePair("image", image_str)); nameValuePairs.add(new BasicNameValuePair("smallImage", smallimage_str)); System.gc(); }
/** Callback invoked when the surface dimensions change. */ public void setSurfaceSize(int width, int height) { // synchronized to make sure these all change atomically synchronized (mGameSurfaceHolder) { mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, width, height, true); } }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int height = bottom - top; int width = right - left; // Log.d(TAG, String.format("Height: %d Width: %d", height, width)); float sizeMultiplier = 1.0f; int forcedPadding = 10; // determine the position of the background image first // then all other items will depend on this one // for now, we will just use simple midpoint adjustment int pos_x = 0; if (mBackground.getWidth() < width) { pos_x = (width - mBackground.getWidth()) / 2; } else if (mBackground.getWidth() > width) { sizeMultiplier = (float) (width) / (float) mBackground.getWidth(); mBackground = Bitmap.createScaledBitmap( mBackground, (int) (mBackground.getWidth() * sizeMultiplier), (int) (mBackground.getHeight() * sizeMultiplier), true); pos_x = (width - mBackground.getWidth()) / 2; } int pos_y = 0; if (mBackground.getHeight() < height) { pos_y = (height - mBackground.getHeight()) / 2; } else if (mBackground.getHeight() > height) { sizeMultiplier = (float) (height - forcedPadding) / (float) mBackground.getHeight(); mBackground = Bitmap.createScaledBitmap( mBackground, (int) (mBackground.getWidth() * sizeMultiplier), (int) (mBackground.getHeight() * sizeMultiplier), true); pos_y = (height - mBackground.getHeight()) / 2; } mBackgroundPosition = new Point(pos_x, pos_y); // determine the position of the color wheel pos_y = 0; if (sizeMultiplier < 1.0f) { mWheel = Bitmap.createScaledBitmap( mWheel, (int) (mWheel.getWidth() * sizeMultiplier) - forcedPadding, (int) (mWheel.getHeight() * sizeMultiplier) - forcedPadding, true); } if (mWheel.getHeight() < height) { pos_y = (height - mWheel.getHeight()) / 2; } // put the wheel in the center, taking into account the brightness bar pos_x = (int) (mBackgroundPosition.x + (mBackground.getWidth() - mWheel.getWidth() - SPACE_BETWEEN_WHEEL_AND_BAR - ARC_WIDTH) / 2); mRadius = mWheel.getWidth() / 2.0; mWheelPosition = new Point(pos_x, pos_y); mWheelCenter = new Point(mWheelPosition.x + (int) mRadius, mWheelPosition.y + (int) mRadius); // make the Path for the arc // find the angle for the inner arc double angle = Math.acos(mRadius / (mRadius + (double) SPACE_BETWEEN_WHEEL_AND_BAR)); // change angle to degrees angle *= (180.0 / Math.PI); // get the complementary angle (which is the one we want) angle = 90 - angle; // Log.d(TAG, String.format("Angle: %f", angle)); // make the rects that hold the ovals for the arcs // the first (inner) will have a radius equal to the sum of the color wheel radius // and the space between the color wheel and the brightness bar mInnerArcRect = new RectF( mWheelPosition.x - SPACE_BETWEEN_WHEEL_AND_BAR, mWheelPosition.y - SPACE_BETWEEN_WHEEL_AND_BAR, mWheelPosition.x + mWheel.getWidth() + SPACE_BETWEEN_WHEEL_AND_BAR, mWheelPosition.y + mWheel.getHeight() + SPACE_BETWEEN_WHEEL_AND_BAR); // the second (outer) will have a radius equal to the sum of the inner radius // and the width of the brightness bar mOuterArcRect = new RectF( mInnerArcRect.left - ARC_WIDTH, mInnerArcRect.top - ARC_WIDTH, mInnerArcRect.right + ARC_WIDTH, mInnerArcRect.bottom + ARC_WIDTH); // create a path to hold the bar drawing mArcPath = new Path(); // add the first arc mArcPath.arcTo(mInnerArcRect, (float) (angle * -1.0), (float) (2.0 * angle)); // find the angle for the outer arc angle = Math.acos(mRadius / (mRadius + (double) SPACE_BETWEEN_WHEEL_AND_BAR + (double) ARC_WIDTH)); // change angle to degrees angle *= (180.0 / Math.PI); // get the complementary angle (which is the one we want) angle = 90 - angle; // Log.d(TAG, String.format("Angle: %f", angle)); // add the second arc (a line is added in between the end points of the arcs) mArcPath.arcTo(mOuterArcRect, (float) angle, (float) (-2.0 * angle)); mArcPath.close(); }