2014-09-04 29 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" />