2014-09-04 69 views
2

在一個項目中,我必須在不同的shape中顯示個人資料圖片和其他圖片。我不知道如何創建一個像它的形狀,以及如何在其中顯示圖像。我也必須在列表視圖中放置這種類型的形狀。請建議我。如何創建可以顯示圖像的客戶形狀?

在此先感謝。

+0

Android或iOS? – neteinstein 2014-09-04 12:22:28

+0

我得到了iOS的解決方案,請告訴我關於android。 – 2014-09-04 12:54:42

回答

1

嘗試類似的東西:

profileImageview=[[UIImageView alloc]initWithFrame:CGRectMake(2,10,100,80)]; 
    UIBezierPath *path = [UIBezierPath new]; 
    [path moveToPoint:(CGPoint){0, 0}]; 
    [path addLineToPoint:(CGPoint){100, 0}]; 
    [path addLineToPoint:(CGPoint){70, 80}]; 
    [path addLineToPoint:(CGPoint){0, 80}]; 
    [path addLineToPoint:(CGPoint){0, 0}]; 

    CAShapeLayer *mask = [CAShapeLayer new]; 
    mask.frame = profileImageview.bounds; 
    mask.path = path.CGPath; 

    // Mask the imageView's layer with this shape 
    profileImageview.layer.mask = mask; 
+0

哦,謝謝,它的工作:)。 – 2014-09-05 10:03:19

0

根據需要使用bezier路徑,使用bazie rpaths創建需要的圖形層,並將其添加爲圖像視圖的子圖層。

例如圓形ImageView的代碼如下:

 UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.width/2, self.height/2) radius:MIN(self.width, self.height)/2 startAngle:0.0f endAngle:2 * M_PI clockwise:YES]; 
    CAShapeLayer *maskCircularShapeLayer = [CAShapeLayer layer]; maskCircularShapeLayer.path = path.CGPath; 
    [self.layer addSublayer:maskCircularShapeLayer]; 
0

我試過找來的Android解決方案。我分享我做的任何事情。

  • 我剛剛創建一個類CUSTOMSHAPE是擴展查看類。
  • 重寫onDraw()方法。
  • 創建一個Paint和Path對象。
  • 將直線畫成座標。
  • 從資源創建一個位圖來顯示圖像內部形狀。
  • 創建一個BitmapShader對象並將其設置爲Paint。
  • 用路徑和畫圖對象繪製畫布。
  • 在XML中創建佈局並添加CustomeShape。
  • 創建和實例化的CustomShape對象。

這裏是CUSTOMSHAPE類的代碼:

public class CustomShape extends View { 
Bitmap bitmap; 
BitmapShader bitmapShader; 

public CustomShape(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 

} 

public CustomShape(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomShape(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 


@Override 
protected void onDraw(Canvas canvas) { 
    // TODO Auto-generated method stub 
    super.onDraw(canvas); 

    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 
    Path pth = new Path(); 
    pth.moveTo(0, 0); 

    pth.lineTo(100, 0); 
    pth.lineTo(70, 100); 
    pth.lineTo(0, 100); 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ppp); 
    bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, 
      Shader.TileMode.REPEAT); 
    p.setShader(bitmapShader); 
    canvas.drawPath(pth, p); 
} 

這裏是MainActivity.java

public class MainActivity extends Activity { 

CustomShape customShape; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_main); 

    customShape = (CustomShape) findViewById(R.id.customeShape); 

    } 

} 

這裏的代碼佈局

<com.example.btndemo.CustomShape 
     android:id="@+id/customeShape" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />