Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nine-patch content area not working

I'm trying to make a frame for TextView as a cloud. But the content area does not behave as expected. What am i doing wrong?enter image description here

nine-patch not working

enter image description here

like image 271
ahtartam Avatar asked Aug 13 '26 23:08

ahtartam


1 Answers

I have a suggestion that is not working properly because the content area less scale area. So sad. I remade it to handle 9-patch manually. Save pictures without .9.png. Get Bitmap. There are 9-line present. With getPixels calculated padding and set it on the TextView. After that calculating and set LayoutParams.width and LayoutParams.height. Looks a bit ugly, but it works quite quickly, and most importantly correctly.

private int startX=-1;
private int endX=-1;
private int contentW=-1;
private int contentH=-1;

Bitmap bmp=BitmapFactory.decodeResource(getResources(), mIconResId);
int[] pixels=new int[bmp.getWidth()*bmp.getHeight()];
bmp.getPixels(pixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(),bmp.getHeight());
for(int i=0;i<bmp.getWidth();i++){
  if(startX==-1 && pixels[bmp.getWidth()*(bmp.getHeight()-1)+i]==Color.BLACK){
    startX=i;
  }
  if(startX!=-1 && pixels[bmp.getWidth()*(bmp.getHeight()-1)+i]!=Color.BLACK){
    endX=i;
    break;
  }
}
int startY=-1;
int endY=-1;
for(int i=0;i<bmp.getHeight();i++){
  if(startY==-1 && pixels[bmp.getWidth()*(i+1)-1]==Color.BLACK){
    startY=i;
  }
  if(startY!=-1 && pixels[bmp.getWidth()*(i+1)-1]!=Color.BLACK){
    endY=i;
    break;
  }
}

setBackground(new BitmapDrawable(getResources(),Bitmap.createBitmap(bmp, 1, 1, bmp.getWidth()-2, bmp.getHeight()-2)));

contentW=endX-startX;
endX=bmp.getWidth()-endX;
contentH=endY-startY;
endY=bmp.getHeight()-endY;

new Handler().post(new Rannable(){
@Override
public void run() {
  int w=textview.getWidth();
  int h=textview.getHeight();

  if(w>endX-startX){
    float k=((float)w)/contentW;
    startX=(int) (startX*k);
    endX=(int) (endX*k);
  }
  if(h>endY-startY){
    float k=((float)h)/contentH;
    startY=(int) (startY*k);
    endY=(int) (endY*k);
  }

  w+=startX+startX;
  h+=startY+endY;
  textview.setPadding(startX, startY, endX, endY);
  LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w,h);
  textview.setLayoutParams(lp);
}
});
like image 92
ahtartam Avatar answered Aug 15 '26 13:08

ahtartam